Adding number validation
When collecting data from a user, there are many situations when you will want to only allow numbers in a form field. Examples of this could be telephone numbers, PIN codes, or ZIP codes, to name a few. This recipe will show you how to validate the telephone number field within the form we created in the previous recipe.
Getting ready
Ensure that you have completed the previous recipe and have the same files available. Open validation.js
in your text editor or IDE of choice.
How to do it…
Add number validation to the form you created in the previous recipe by performing the following steps:
Update
validation.js
to be as follows, adding thevaldiateNumber()
function with an additionalhasClass('number')
check inside thefor
loop:$(function(){ $('.submit-btn').click(function(event){ //Prevent form submission event.preventDefault(); var inputs = $('input'); var isError = false; //Remove old errors $('.input-frame').removeClass('error...