Results 1 to 3 of 3

Thread: multiple field checking

  1. #1
    Join Date
    May 2009
    Location
    Greensboro, GA
    Posts
    163
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default multiple field checking

    I have a lot of field checks on a form. If the user doesn't fill in required field and hits submit, he gets an error message popup for EVERY error. If the user didn't answer 9 questions, he gets 9 popups.

    What I would like is: If the user doesn't fill in a required field and hits submit, he gets a popup error and focus is given to the field that needs to be input. That makes it a lot easier for the user.

    I hope I explained this right. Below is partial code.

    Code:
    <script type="text/javascript">
    function validate() {
    mNv1=dogadoptionform.xname.value;
    mNv2=dogadoptionform.xemail.value;
    mNv3=dogadoptionform.xaddress.value;
    mNv4=dogadoptionform.xcity.value;
    .... lots more....
    
    if (mNv1=='') {
    alert('Name is required.');
    event.returnValue=false;
    }
    
    if (mNv2=='') {
    alert('Email is required.');
    event.returnValue=false;
    }
    if (mNv3=='') {
    alert('Address is required.');
    event.returnValue=false;
    }
    if (mNv4=='') {
    alert('City is required.');
    event.returnValue=false;
    }
    .... lots more.....
    </script>
    Last edited by mcolton; 08-26-2009 at 12:45 PM.

  2. #2
    Join Date
    Jul 2009
    Location
    Washington (USA)
    Posts
    94
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default Huh?

    I'm a littile confused, will this script help?

    HTML Code:
    <script language='javascript'>
    function verifyMe(){
    var msg='';
    
    if(document.getElementById('name').value==''){
    	msg+='- Name\n\n';}
    
    var email=document.getElementById('email').value;
    if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))){
    msg+='- Invalid Email Address: '+email+'\n\n';}
    
    if(document.getElementById('email').value==''){
    	msg+='- Email\n\n';}
    
    if(document.getElementById('subject').value=='ns'){
    	msg+='- Subject\n\n';}
    
    if(document.getElementById('body').value==''){
    	msg+='- Message\n\n';}
    
    if(msg!=''){
    	alert('The following fields are empty or invalid:\n\n'+msg);
    	return false
    }else{
    	return true }
    
    }
    </script>

  3. #3
    Join Date
    May 2009
    Location
    Greensboro, GA
    Posts
    163
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    Thanks. It works great
    Last edited by mcolton; 08-26-2009 at 12:45 PM.

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
  •