First, let’s start by building the webpage and the validation code then we will talk about the regular expressions used to validate the strings.
First go to your html file and paste the following code : Here, I have a simple form.
It has an onsubmit event attached to a function called validate() that we are going to create later inside this form there are three inputs one for our phone number another one is for postal codes and the third and the last one is a submit button that is going to submit the form after we are done filling it. It contains only the code that validates the phone number.
Through a list of examples , we will build a script to validate phone numbers , UK postal codes, along with more examples.So basically the expression tries to match with a phone number like this (541) 754-3010 or like this 541-754-3010 or with spaces Now let’s move on to the Postal code part.Here I have added a new variable called postal Code to fetch the postal code from the form then created another variable to store the postal code regular expression which we will be discussing in a second.So here is the Regular expression pattern:/^[A-Z][0-9] ?[0-9][A-Z]$/i The i at the end of the pattern is to indicate that this is a case insensitive match.