Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Nov 2006
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with simple js Form Validation

    Hi,
    I have a simple form.
    I checked if the mail is valid and if they filled the name field.

    The problem is that i want to let the client to fill their phone or mobile number.
    I did a flag but it doesn't work. something is realy wrong with the code and i don't know why?

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    
    <!-- Generated by responder.co.il -->
    <script language="JavaScript" type="text/javascript">
    <!--
    function responder_validation()
    {
        var flag = false
        var rspform = document.getElementById("rspform");
        var listitems= rspform.getElementsByTagName("input");
    
    
    
    for (a=0;a<listitems.length;a++)
    { switch (listitems[a].name)
      {
    
    	case 'fields[subscribers_email]':	var reEmail = /^[-\w\d\.]+@[-\w\d\.]+\.[-\w\d\.]+$/;
    										if(listitems[a].value == "")
    											{   alert("please fill in your email");listitems[a].focus();return false;}
    										if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(listitems[a].value)))
    											{   alert("your email is not correct.");listitems[a].focus();return false;}
    										break;
    	case "fields[subscribers_name]":			if(listitems[a].value == "")
    											{   alert("Please fill in your name");listitems[a].focus();return false;}
    										break;
    
    
    
    	case "fields[phone]":			if(listitems[a].value != "")
    											{   flag=true;}							
    
    	case "fields[mobile]":			if(listitems[a].value != "")
    											{  flag=true;}
    
    										
    	case "fields[subject]":		
    									if(listitems[a].value == "")
    										{   alert("Please right the subject");listitems[a].focus();return false;}
    								break;
    											
    
      }
    }
    					
    					
    if (flag==false)
    {
    	alert("Please fill in your Home phone or mobile number");
    	return false;
    
    }
    
    	}
    				//-->
    				</script>
    
    </head>
    
    <body>
        <form action="http://cp.responder.co.il/subscribe.php" method="post" onSubmit="return responder_validation()" id="rspform" >				
        <table id="tform" align="right" cellpadding="0" cellspacing="5" border="0">
            <tr>
                <td id="label">first name:</td>
                <td id="control"><input id="inputText" name="fields[subscribers_name]" type="text" maxlength="20"></td>
            </tr>
            <tr>
                <td id="label">family name:</td>
                <td id="control"><input id="inputText" name="fields[family_name]" type="text" maxlength="30" /></td>
            </tr>
            <tr>
                <td id="label">email:</td>
                <td id="control"><input id="inputText" name="fields[subscribers_email]" type="text" maxlength="50" /></td>
            </tr>
    
            <tr>
                <td id="label">phone:</td>
                <td id="control"><input id="inputPhone" name="fields[phone]" type="text" maxlength="20" /> </td>
            </tr>
    
            <tr>
                <td id="label">mobile:</td>
                <td id="control"><input id="inputPhone" name="fields[mobile]" type="text" maxlength="20" /> </td>
            </tr>
    
            <tr>
                <td id="label">subject:</td>
                <td id="control"><input id="inputText" name="fields[subject]" type="text" maxlength="50" /></td>
            </tr>
    
            <tr>
                <td id="label">comments:</td>
                <td id="control"><textarea id="inputTextArea" name="fields[comments]"></textarea></td>
            </tr>
            <tr>
                <td id="label"></td>
                <td id="control"><input type="submit" value="send">  
    
                <input type="reset" name="clear" value="clear" id="inputButton"></td>
            </tr>
        </table>
    
    <!--<input type="hidden" name="form_id" value="">-->
    </form>
    </body>
    </html>
    There is also problem with the focus.

    Help...

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    the missing breaks for fields[phone] and fields[mobile]
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    New Coder
    Join Date
    Nov 2006
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    thank you very very much

    It was a stupid mistake and i didn't see it.
    I was looking on it for 3 days.


  •  

    Posting Permissions

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