Jump to content

how to specify if empty=ok or if not empty,must match a regex?


Recommended Posts

Using the exactly logic you have laid out you could use...

 

if( empty( $input ) ) {
} elseif( preg_match( '#^[A-Za-z]{1,15}$#', $input ) ) {
} else {
  $error['input'] = 'Something';
} 

A neater way would probably be...

 

if(!empty( $input ) && !preg_match( '#^[A-Za-z]{1,15}$#', $input )) {
   $error['input'] = 'Something';
}

Using the exactly logic you have laid out you could use...

 

if( empty( $input ) ) {
} elseif( preg_match( '#^[A-Za-z]{1,15}$#', $input ) ) {
} else {
  $error['input'] = 'Something';
} 

A neater way would probably be...

 

if(!empty( $input ) && !preg_match( '#^[A-Za-z]{1,15}$#', $input )) {
   $error['input'] = 'Something';
}

 

thanks, guess that works.

also found a method within regex:

preg_match_all("/(^$)|(^[A-Za-z]{1,15}$)/",$input,$match);

 

what do you think of this OR within the regex?

I nearly proffered that as a third option, however it won't actually give quite the same results as using empty(), so I stuck with what you'd actually stated. A value of 0 (or '0') will be considered empty but will fail your Regex, depending on requirements this may be closer to what you want, impossible for me to say. Also why did you use preg_match_all? Given your stated usage preg_match would be the function you want.

I nearly proffered that as a third option, however it won't actually give quite the same results as using empty(), so I stuck with what you'd actually stated. A value of 0 (or '0') will be considered empty but will fail your Regex, depending on requirements this may be closer to what you want, impossible for me to say. Also why did you use preg_match_all? Given your stated usage preg_match would be the function you want.

 

yes, the 0 should fail, and by my requirment it must be so.but i understand what you are saying in a generic sense.its a new insight-thanks. yes i need preg_match.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.