SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    Non-Member
    Join Date
    Mar 2005
    Posts
    832
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript form validation

    Hi i have got this code here and when i press submit i want it to go to the page called validate.htm could anyone please help me?

    Thank you.

    Code:
    <script type='text/javascript'>
    
    function formValidator(){
    	// Make quick references to our fields
    	var firstname = document.getElementById('firstname');
    	var lastname = document.getElementById('lastname');
    	var addr = document.getElementById('addr');
    	var suburb = document.getElementById('suburb');
    	var state = document.getElementById('state');
    	var postcode = document.getElementById('postcode');
    	var loanamount = document.getElementById('loanamount');
    	var loanduration = document.getElementById('loanduration');
    	
    	// Check each input in the order that it appears in the form!
    	if(isAlphabet(firstname, "Please enter only letters for your first name")){
    	if(isAlphabet(lastname, "Please enter only letters for your last name")){
    		if(isAlphanumeric(addr, "Numbers and Letters Only for Address")){
    	if(isAlphabet(suburb, "Please enter only letters for your suburb")){
    				if(madeSelection(state, "Please Choose a State")){
    			if(isNumeric(postcode, "Please enter a valid post code using only numbers")){
    			if(isNumeric(loanamount, "Please enter only numbers")){
    			if(isNumeric(loanduration, "Please enter a number")){
    							return true;
    						}
    					}
    				}
    			}
    		}
    	}
    	}
    	}
    	
    	
    	return false;
    	
    }
    
    function isEmpty(elem, helperMsg){
    	if(elem.value.length == 0){
    		alert(helperMsg);
    		elem.focus(); // set the focus to this input
    		return true;
    	}
    	return false;
    }
    
    function isNumeric(elem, helperMsg){
    	var numericExpression = /^[0-9]+$/;
    	if(elem.value.match(numericExpression)){
    		return true;
    	}else{
    		alert(helperMsg);
    		elem.focus();
    		return false;
    	}
    }
    
    function isAlphabet(elem, helperMsg){
    	var alphaExp = /^[a-zA-Z]+$/;
    	if(elem.value.match(alphaExp)){
    		return true;
    	}else{
    		alert(helperMsg);
    		elem.focus();
    		return false;
    	}
    }
    
    function isAlphanumeric(elem, helperMsg){
    	var alphaExp = /^[0-9a-zA-Z]+$/;
    	if(elem.value.match(alphaExp)){
    		return true;
    	}else{
    		alert(helperMsg);
    		elem.focus();
    		return false;
    	}
    }
    
    function lengthRestriction(elem, min, max){
    	var uInput = elem.value;
    	if(uInput.length >= min && uInput.length <= max){
    		return true;
    	}else{
    		alert("Please enter between " +min+ " and " +max+ " characters");
    		elem.focus();
    		return false;
    	}
    }
    
    function madeSelection(elem, helperMsg){
    	if(elem.value == "Please Choose"){
    		alert(helperMsg);
    		elem.focus();
    		return false;
    	}else{
    		return true;
    	}
    }
    
    function emailValidator(elem, helperMsg){
    	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
    	if(elem.value.match(emailExp)){
    		return true;
    	}else{
    		alert(helperMsg);
    		elem.focus();
    		return false;
    	}
    }
    </script>
    
    <form onsubmit='return formValidator()' >
    First Name: <input type='text' id='firstname' /><br />
    Last Name: <input type='text' id='lastname' /><br />
    Address: <input type='text' id='addr' /><br />
    Suburb: <input type='text' id='suburb' /><br />
    State: <select id='state'>
    	<option>Please Choose</option>
    	<option>NSW</option>
    	<option>QLD</option>
    	<option>SA</option>
    	<option>TAS</option>
    	<option>VIC</option>
    	<option>WA</option>
    </select><br />
    Post Code: <input type='text' id='postcode' /><br />
    Loan Amount: <input type='text' id='loanamount' /><br />
    Loan Duration: <input type='text' id='loanduration' /> of years<br />
    <input type='submit' value='Check Form' />
    </form>

  2. #2
    SitePoint Evangelist vikrantkorde's Avatar
    Join Date
    Jun 2004
    Location
    Mumbai, India
    Posts
    541
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you just need to change ur form element as follows


    <form onsubmit='return formValidator()' action='validate.htm'>

    i have just added the action='validate.htm'
    this was the entry that was missing.
    Vikrant Korde
    S Y S T I M E, Mhape,
    Mumbai, Maharashtra, India.

  3. #3
    Non-Member
    Join Date
    Mar 2005
    Posts
    832
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    where abouts would i put that


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •