Regular expression for validating numbers only in javascript


10-Jun-2015 02:27

Java Script has an alternate syntax for creating Regular Expression objects that implicitly calls the Reg Exp constructor function.

But these patterns used in Reg Exp can be very simple.

Fortunately, Java Script 1.2+ has incorporated regular expressions.

After reading this article you’ll be able to do advanced javascript validation using regular expressions a.k.a.

Reg Exp for basic javascript validation read Javascript Validation – Textbox Combobox Radiobutton Checkbox.

Here we create a Reg Exp pattern and use the match to validate it.In a previous post trim a string from white space by Java Script function ( or Another Java Script function for trim a string from white space ) and Java Script function-Splits the string by given separator and returns an array with trimmed items, Some Regular expressions have been used.So the condition is we check whether the textbox contains anything ELSE other than numbers and display a message As you can see the function validate() checks if the entered string contains characters that does NOT (notice the ^ symbol) match the numbers 0 to 9 including white spaces and special characters.From numbers we’ll move to alphabets first an example for validation lowercase alphabets and then to validate both uppercase and lowercase.

Regular expressions are very powerful tools for performing pattern matches.And validating user input is the bane of every software developer existence.