vijdev Posted June 19, 2010 Share Posted June 19, 2010 how to specify: if (an input field is empty) { it is OK dont set $error['input'] } elseif(matches ^[A-Za-z]{1,15}$) { it is OK dont set $error['input'] } else{ set $error[input'] } in other words match empty or match regex for no error case Quote Link to comment https://forums.phpfreaks.com/topic/205236-how-to-specify-if-emptyok-or-if-not-emptymust-match-a-regex/ Share on other sites More sharing options...
cags Posted June 19, 2010 Share Posted June 19, 2010 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'; } Quote Link to comment https://forums.phpfreaks.com/topic/205236-how-to-specify-if-emptyok-or-if-not-emptymust-match-a-regex/#findComment-1074320 Share on other sites More sharing options...
vijdev Posted June 19, 2010 Author Share Posted June 19, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/205236-how-to-specify-if-emptyok-or-if-not-emptymust-match-a-regex/#findComment-1074335 Share on other sites More sharing options...
cags Posted June 19, 2010 Share Posted June 19, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/205236-how-to-specify-if-emptyok-or-if-not-emptymust-match-a-regex/#findComment-1074337 Share on other sites More sharing options...
vijdev Posted June 20, 2010 Author Share Posted June 20, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/205236-how-to-specify-if-emptyok-or-if-not-emptymust-match-a-regex/#findComment-1074504 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.