JavaScript Tutorial

adplus-dvertising
Validation In Javascript
Previous Home Next

javascript provide a several type of validation and its also provide difference facilities in the validation forms The idea behind JavaScript form validation is to provide a method to check the user entered information before they can even submit it .

JavaScript also lets you display helpful alerts to inform the user what information they have entered by user that's correct or in correct .

form validation example :

Form validation is the process of checking that a form has been filled in correctly before it is processed.

<html>
<head>    

<script>  
       function validateform(){  
        var name=document.myform.name.value;  
        var password=document.myform.password.value;  
      
            if (name==null || name==""){  
               alert("Name can't be blank");  
                 return false;  
    }else if(password.length<10){  
             alert("Password must be at least 10 characters long.");  
      return false;  
      }  
    }  
    </script>  
    <body>  
    <form name="myform" method="post" action=" " onsubmit=
	"return validateform()" >  
    Name: <input type="text" name="name"><br/>  
    Password: <input type="password" name="password"><br/>  
    <input type="submit" value="register">  
    </form> 
</html>
</head>   

Output :

Number Validation :

<html>
<head>

<script >
    function lengthRestriction(elem, min, max){
	var Input = elem.value;
	if(Input.length >= min && Input.length <= max){
		return true;
	}else{
		document.write (
		"Please enter between " +min+ " and " 
		
		+max+ " characters");
		
		return false;
	}
}
</script>
<form>
Username(10-50 
characters): <input type='text' id='prasun'
/>
<input type='button' 
	onclick="lengthRestriction(document.getElementById('prasun'), 10, 50)
	"value='button' />
</form>
</head>
</html>

Output :

Please enter between 10 and 50 characters

Previous Home Next