Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Simple Javascript form validation

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 427
    Comment on it

    Here i have written JS code to validate form, for example:
    checking empty fields, checking email, password user-name etc.

    function valid()
    		{
    			var name = document.getElementById('name').value;
    			var passwd = document.getElementById('password').value;
    			var email = document.getElementById('email').value;
    			var mobile = document.getElementById('mobile').value;
    			var chk_nam=chk_name(name);
    			var chk_emai=chk_email(email);
    			var chk_passw=chk_passwd(passwd);
    			var chk_mobil=chk_mobile(mobile);
    			// alert(chk_empty(name,passwd,email,mobile));
    			if(!chk_empty())
    			{
    				alert("Feilds cannot be left blank");
    				return false;
    			}
    			else if (!chk_name(name.trim()))
    			{
    				alert("Error: Only alphabets and space are allowed ");
    				document.getElementById('name').focus();
    				document.getElementById('name').style.borderColor = "red";
    				document.getElementById('name').style.boxShadow = "0px 0px 7px 2px red";
    				return false;
    			}
    			else if(!chk_email(email.trim()))
    			{
    				alert("Error: Invalid Email");
    				document.getElementById('email').focus();
    				document.getElementById('email').style.borderColor = "red";
    				document.getElementById('email').style.boxShadow = "0px 0px 7px 2px red";
    				return false;
    			}
    			else if(!chk_passwd(passwd.trim()))
    			{
    				alert("Error: Password should atleast contain a alphabets,number and special characters and minimun length sholud be 6 characters"+passwd);
    				document.getElementById('password').focus();
    				document.getElementById('password').style.borderColor = "red";
    				document.getElementById('password').style.boxShadow = "0px 0px 7px 2px red";
    				return false;
    			}
    			else if(!chk_mobile(mobile.trim())) 
    			{
    				alert("Error: Invalid Mobile number");
    				document.getElementById('mobile').focus();
    				document.getElementById('mobile').style.borderColor = "red";
    				document.getElementById('mobile').style.boxShadow = "0px 0px 7px 2px red";
    				return false;
    			}
    			else
    				return true;
    		}
    		function chk_empty()
    		{
    			var i=0;
    			var flag=false;;
    			for(i=0;i<4;i++)
    			{
    				if(document.getElementsByTagName('input')[i].value=='')
    				{
    					document.getElementsByTagName('input')[i].focus();
    					document.getElementsByTagName('input')[i].style.borderColor = "red";
    					document.getElementsByTagName('input')[i].style.boxShadow = "0px 0px 7px 2px red";
    					flag=false;
    				}
    				else{
    					document.getElementsByTagName('input')[i].style.borderColor = "#222";
    					document.getElementsByTagName('input')[i].style.boxShadow = "none";
    					flag=true;
    				}
    			}
    			return flag;
    		}
    		function chk_name(name)
    		{
    			var pattern = /^[A-z\s]*$/;
    			if(pattern.test(name))
    				return true;
    			else
    				return false;
    		}
    		function chk_email(email)
    		{
    			var pattern = /^[a-zA-Z]+([\._] [a-z0-9]+)*@[a-z]+([\.]?[a-z]+)*(\.[a-z]{2,4})+$/;
    			if(pattern.test(email))
    				return true;
    			else
    				return false;
    		}
    		function chk_passwd(passwd)
    		{
    			/*(/^
            	(?=.*\d)                //should contain at least one digit
            	(?=.*[a-z])             //should contain at least one lower case
            	(?=.*[A-Z])             //should contain at least one upper case
            	(?=.*[!@#$%^&*])		//should contain at least one special characters
            	[a-zA-Z0-9]{8,}         //should contain at least 8 character from the mentioned characters
    
            	$/)*/
    			
    			var pattern = /^(?=.*\d)(?=.*[a-zA-Z])(?=.*[!@#$%^&*_])[a-zA-Z0-9_!@#$%^&*]{6,}$/;
    			if (pattern.test(passwd))
    				return true;
    			else
    				return false;
    		}
    		function chk_mobile(mobile)
    		{
    			var pattern = /^[7-9]+[0-9]*$/;
    			if(pattern.test(mobile))
    				return true;
    			else
    				return false;
    		}

    As you can see i have made a different function checking different type of validation.

    Here i have regular expression to perform pattern-matching for various inputs that user may enter.

    Simple Javascript form validation

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: