Q:

Allow only leading and trailing spaces with Numbers

I need to have a textbox which accepts below:

Allow only Numeric with length of 6 digits
Range from 000001-999999
Allow only leading and trailing spaces (not between digits)

A:

It can have space at start then 6 digit then can have space.
You should perform one more check like this.

^ should start with
\s* 0 or more space
[0-9]{6} 6 digits
\s* 0 or more space
$ should end with

var re=/^\s?[0-9]{4,6}\s?$/;
function check(a){
  console.log(re.test(a) && a.length==6);
}
check('123456'); // true
check(" 1234 "); // true
check(" 12345"); // true
check("     "); // false
check(" 12345 "); //false
check("12345 "); // true

