woolyg Posted July 7, 2008 Share Posted July 7, 2008 Hi all, I'm looking to disallow all characters except the following from a form's text field: 1 2 3 4 5 6 7 8 9 0 W L D Can anyone help me with the preg_match? WoolyG Quote Link to comment https://forums.phpfreaks.com/topic/113522-speicific-preg_match-help-pls/ Share on other sites More sharing options...
btherl Posted July 7, 2008 Share Posted July 7, 2008 Here's how I would do it if (preg_match('|^[0-9WLD]+$|', $input)) { # Validated } else { # Failed validation } The above requires at least 1 character to be present to be valid. If you use "*" instead of "+" then it will allow 0 characters to be valid as well. Quote Link to comment https://forums.phpfreaks.com/topic/113522-speicific-preg_match-help-pls/#findComment-583328 Share on other sites More sharing options...
woolyg Posted July 7, 2008 Author Share Posted July 7, 2008 That's great, thanks. There'll always be at least 1 character being entered, so I'll use what you printed! Nice one, thanks again - WoolyG Quote Link to comment https://forums.phpfreaks.com/topic/113522-speicific-preg_match-help-pls/#findComment-583334 Share on other sites More sharing options...
woolyg Posted July 8, 2008 Author Share Posted July 8, 2008 An addition to my query: Say if I wanted to invalidate $input if preg_match had BOTH numerals and characters (W/L/D) in it, how would I go about that? Cheers, WoolyG Quote Link to comment https://forums.phpfreaks.com/topic/113522-speicific-preg_match-help-pls/#findComment-584239 Share on other sites More sharing options...
effigy Posted July 8, 2008 Share Posted July 8, 2008 This will match only digits or characters: /^(?:\d+|[WLD]+)$/ Quote Link to comment https://forums.phpfreaks.com/topic/113522-speicific-preg_match-help-pls/#findComment-584456 Share on other sites More sharing options...
woolyg Posted July 9, 2008 Author Share Posted July 9, 2008 I'm trying to invalidate an entry that uses numerals and any of the characters D/W/L. Would this work, below? <?php if( (preg_match('|^[WLD]+$|', $input)) && (preg_match('|^[WLD]+$|', $input)) ) { // Invalidate $input } ?> .. is the code above ok? That is, if $input had number(s) in its string, and had W/L/D in its string, it's invalid? Cheers, WoolyG Quote Link to comment https://forums.phpfreaks.com/topic/113522-speicific-preg_match-help-pls/#findComment-585014 Share on other sites More sharing options...
btherl Posted July 9, 2008 Share Posted July 9, 2008 What effigy wrote is what you're looking for. His regexp translates to "$input contains only digits or $input contains only WLD". So it will not validate an $input that contains both WLD and digits. Basically it does everything all in one, validation of WLD *or* digits, as well as invalidation of anything else. Quote Link to comment https://forums.phpfreaks.com/topic/113522-speicific-preg_match-help-pls/#findComment-585079 Share on other sites More sharing options...
woolyg Posted July 9, 2008 Author Share Posted July 9, 2008 OK, thanks for that - WoolyG Quote Link to comment https://forums.phpfreaks.com/topic/113522-speicific-preg_match-help-pls/#findComment-585082 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.