1702470332

Validation of javascript forms - name, password, password retype validation and Number Validation


It is critical to check the user-submitted form since it may include incor- rect information. As a result, validation is required to authenticate the user. Because JavaScript allows form validation on the client side, data processing is faster than server-side validation.15 JavaScript form validation is preferred by the majority of web developers. We can validate name, password, email, date, cell numbers, and other data using JavaScript. Example of Form Validation in JavaScript In this example, we’ll validate the name and password. The name cannot be blank, and the password cannot be less than seven characters long. On form submission, we validate the form. The user will not be sent to the next page unless the values entered are correct. ```js function validateForm(){ var name=document.my_form.name.value; var password=document.my_form.password.value; if(name==null || name==""){ alert("Name cannot be blank"); return false; }else if(password.length < 7){ alert("Password must be at least 7 characters long."); return false; } } html <form name="my_form" onSubmit="return validateForm()"> Name: <input type="text" name="name"> Password: <input type="password" name="password"> <input type="submit" value="Register"> </form> ``` password retype validation in javascript ```js function matchpass(){ var firstpassword=document.fm1.password.value; var secondpassword=document.fm1.password2.value; if(firstpassword==secondpassword){ return true; }else{ alert("Password must same!"); return false; } } html <form name="fm1" onSubmit="return matchpass()"> Password: <input type="password" name="password"> Re-enter password: <input type="password" name="password2"> <input type="submit"> </form> ``` Number Validation in JavaScript Let us verify the textfield solely for numeric values. We’re using the isNaN() method here. ```js function validate(){ var num=document.my_form.num.value; if(isNaN(num)){ document.getElementById("numloc").innerHTML="Enter Numeric value only"; return false; }else{ return true; } } html <form name="my_form" onSubmit="return validate()"> <input type="text" name="num"><span id="numloc"></span> <input type="submit"> </form> ``` Email Validation in JavaScript Using JavaScript, we can validate the email. To validate the email address, several criteria must be met, including: + The @ and. characters must appear in the email address. + Before and after the @, there must be at least one character. + There must be at least two characters following.(dot). Let’s look at a simple example of email validation. ```js function validateEmail(){ var x=document.my_form.email.value; var atposition=x.indexOf("@"); var dotposition=x.lastIndexOf("."); if(atposition < 1 || dotposition < atposition+2 || dotposition+2 >=x.length){ alert("Enter the valid email address \n atposition:"+atposition+"\n dotposition:"+dotposition); return false; } } html <form name="my_form" onSubmit="return validateEmail()"> Email: <input type="text" name="email"> <input type="submit"> </form> ```

(0) Comments