Validating an input field with regular expression using JavaScript !

A regular expression is a special text for describing a string pattern.

Regular expressions are commonly used to validate user entered input fields like Email Address, Phone number, Name etc. The regular expression to be used depends on the requirement and can be framed.

The following simple javaScript snippet can be used to validate a field with regular expression.



Replace the var regex value with any regular expression you need to validate with. The validateForm() method should be called on the onSubmit event of the form –

<form id=”form1″ name=”form1″ method=”post” onsubmit=”return validateForm()”action=”~/URL”>

Hope this helps.

Thanks for checking out !

All comments.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.