简体   繁体   中英

Regular Expression only digits and blank spaces

I have a text input and I require that it only accepts digits (0-9) and blank spaces (" "). The current regexp to validate this input is:

/((\d{1,})(\s{1,})?){1,}/

Which stands for: one or more groups base on a first group of one or more digits and an optional second group of one or more blank spaces

That will only let me introduce values as: 999999 (only digits) or " " (only blank spaces) or 91 08 8510 903 (mix of digits and spaces). But actually, I also can insert aaa or other characters.

Dissection

Your regular expression doesn't accept only letters :

/((\d{1,})(\s{1,})?){1,}/.test('aaa') // false

Actually, any character is accepted if the input contains at least one digit :

/((\d{1,})(\s{1,})?){1,}/.test('a1a') // true

That being said, let's skim the fat from your pattern :

"{1,}" equals "+"   -> ((\d+)(\s+)?)+
"(.+)?" equals ".*" -> ((\d+)\s*)+
useless brackets    -> (\d+\s*)+

This result can be translated to : "one or more digits ( \\d+ ) followed by zero or more blank spaces ( \\s* ), one or more times ( ()+ ), anywhere in the input ". Alternatively, we could say : "at least one digit, anywhere in the input ".

What you need is to replace "anywhere in the input" with "from the beginning to the end of the input". This is allowed by the following special characters : ^ (beginning of input) and $ (end of input). Let's make a bunch of tests to see how they work :

requirement                                 regex     input   .test()
---------------------------------------------------------------------
must contain at least one digit             /\d+/     'a1a'   true  
must start with at least one digit          /^\d+/    '1a'    true  
must start with at least one digit          /^\d+/    'a1'    false 
must end with at least one digit            /\d+$/    '1a'    false 
must end with at least one digit            /\d+$/    'a1'    true  
only digits from the beginning to the end   /^\d+$/   '1a1'   false

Suggestion

Only digits potentially separated by one whitespace : /^\\d+( \\d+)*$/ .

^         beginning of the input
\d+       a digit, one or more times
( \d+)*   a whitespace + same as above, zero or more times
$         end of the input

Usage example :

var r = /^\d+( \d+)*$/;
var isValid = r.test(' 1 ');  // false
var isValid = r.test('1 1');  // true
var isValid = r.test('1  1'); // false

More about regular expressions : http://www.javascriptkit.com/javatutors/redev.shtml .

try this one

$pattern = '/^[0-9 ]+$/';

  if ( preg_match ($pattern, $text) )
  {
   echo 'allowed';
  }

Try this regular expression

/(\d+\s*\d*)+$/

Visualize the results here http://regex101.com/r/dE5uR8

I have tested it online using

http://regexpal.com/

The above regular expression will not accept empty blank spaces at the start. You need atleast one digit. If you want to match the empty spaces at the start also change it to (\\d*\\s*\\d*)+$ which will accept empty spaces also

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM