Q:

How to validate a Number field in Javascript using Regular Expressions?

Is the following correct?
var z1=^[0-9]*\d$;
{
    if(!z1.test(enrol))
    {
        alert('Please provide a valid Enrollment Number');
        return false;
    }
} 

Its not currently working on my system.

A:

You can test it as:
/^\d*$/.test(value)

Where:

The / at both ends mark the start and end of the regex
The ^ and $ at the ends is to check the full string than for partial matches
\d* looks for multiple occurrences of number charcters

You do not need to check for both \d as well as [0-9] as they both do the same - i.e. match numbers.

A:

var numberRegex = /^\s*[+-]?(\d+|\.\d+|\d+\.\d+|\d+\.)(e[+-]?\d+)?\s*$/
var isNumber = function(s) {
    return numberRegex.test(s);
};

"0"           => true
"3."          => true
".1"          => true
" 0.1 "       => true
" -90e3   "   => true
"2e10"        => true
" 6e-1"       => true
"53.5e93"     => true

"abc"         => false
"1 a"         => false
" 1e"         => false
"e3"          => false
" 99e2.5 "    => false
" --6 "       => false
"-+3"         => false
"95a54e53"    => false

