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

javascript

Solution

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.

Problem

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.

Original source