Blogs


testware_blog_image

How to make a validation using javascript

May 24, 2019 | Indhumathy

Validation is required to prevent web and forms. Improper validation of form data is one of the main causes of insecurity. The form must be checked to make sure all the mandatory fields are filled and it has correct value. Your code must include appropriate logic to test correctness of data. Otherwise it exposes your website to attacks such as hacking, cross-site scripting.

Form validation is nothing but client had entered all the data and then press the submit button.The given data was empty or inappropriate values,error message would through in corresponding fields.Generally this form validation would perform in javascript,jquery etc. In Javascript validation,we are calling return the function called validate() in form onsubmit.

1)The given field was blank,you will get the message error as "this field is required..!".

2)Username field does not use symbols,only we can use alphanumeric value and give condition as /^[a-zA-Z0-9-_]+$/

3)The email address field had format as variable.indexOf("@")<1 || variable.lastIndexOf(".");<variable.indexOf("@")+2 || variable.lastIndexOf(".")+2>=variable.length.It has invalid position message error as "Invalid Email Address..!"

Other than the bussiness email in the field,we do not accept. So that given condition as

var domains = ["gmail","msn","yahoo","yopmail","hotmail"]; //update ur domains here

var index1 = mail.indexOf("@");

if(index1 > -1){

var splitStr = mail.split("@");

var sub = splitStr[1].split(".");

domains.indexOf(sub[0])>-1) given as if condition and return false in the field message error show as Please Enter your Business Email, We do not accept Gmail, Yahoo, Hotmail, Yopmail etc...!";

4)Password field had more validations such as

Username and password must not same. So we give condition as var username == var password , show message error as "Password must be Different from Username...!".

Password length would not give less than 8 character contain eight character ,so we give condition as variable.length  <  8 and show message error as "Password must Contain at least Eight Characters..!"

Password must Contain at least One Number (0-9),so we give condition as variable = /[0-9]/

Password must Contain at least One Uppercase (A-Z),so we give condition as variable =/[A-Z]/

Password must Contain at least One Number (a-z),so we give condition as variable = /[a-z]/

Password must Contain at least One Number (!@#$%^&*_),so we give condition as variable = /[!@#$%^&*_]/

5)Number field would restrict the characters and the condition such as "value=isNaN(parseFloat(value))?'':value"

Therefore the validation should be forcing our users to enter secure passwords, email and other details, it makes easier to protect their account information and general details. User don't have to wait for a page reload to see, because error message will show immediately