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 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2006
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    email validation

    Hi everyone.

    I am using the following code to create a form validation process.

    <script language="JavaScript" type="text/JavaScript">
    <!--
    function MM_validateForm() { //v4.0
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
    if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
    if (p<1 || p==(val.length-1)) errors+='- '+nm+' must be a valid e-mail address.\n';
    } else if (test!='R') { num = parseFloat(val);
    if (isNaN(val)) errors+='- '+nm+' must not contain spaces.\n';
    if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
    min=test.substring(8,p); max=test.substring(p+1);
    if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
    }

    function MM_findObj(n, d) { //v4.01
    var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
    if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
    if(!x && d.getElementById) x=d.getElementById(n); return x;
    }
    //-->
    </script>


    This is the code that dictates the form email input box

    <tr valign="top">
    <td>Email Address </td>
    <td><input name="email" type="text" id="email">
    <font color="#FF0000"> *</font></td>
    </tr>


    This is the code for the phone # input box

    <tr valign="top">
    <td>Phone Number (no spaces) </td>
    <td><input type="text" name="phone">
    <font color="#FF0000">* </font> </td>
    </tr>


    <p><font color="#FF0000"><br>
    * = Required Field</font> </p></td>
    </tr>
    </table>

    Now - problem is that when the submit button is clicked - it will not accept a phone number with spaces in ( I WANT IT TO ) and secondly the email address part will only alert if the field is left blank. ( I WANT IT TO VALIDATE THE EMAIL ADDRESS BY WAY OF CHECKING THE CHARACHTERS USED WHILE DISALLOWING SPACES AND OTHER INVALID INPUTS and then to VERIFY BY WAY OF CHECKING GENUINE DOMAIN ETC ).

    I have done a lot of research and tried altering the following lines as per examples given on tutorial topics but am not getting anywhere.

    function MM_validateForm() { //v4.0
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
    if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
    if (p<1 || p==(val.length-1)) errors+='- '+nm+' must be a valid e-mail address.\n';
    } else if (test!='R') { num = parseFloat(val);


    can anyone help please.

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    There are much simpler solutions to validate a form.
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    <script type="text/javascript">
    function check(form){ //function used to validate contact form
    	if (form.name.value == '') {
    		form.name.focus();
    		alert('You muse enter a name.');
    		return false;
    	}
    	else if (form.email.value == "" || form.email.value == " ") {
    		form.email.focus();
    		alert('You must enter an email.');
    		return false;
    	}
    	else if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(form.email.value))){
    		form.email.focus();
    		alert('Your email is invalid.');
    		return false;
    	}
    	else if(form.phone.value == ''){
    		form.phone.focus();
    		alert('You must enter a phone number.');
    		return false;
    	}
    	else if(!(/^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/.test(form.phone.value))){
    		form.phone.focus();
    		alert('Your phone number was entered incorrectly.');
    		return false;
    	}
    	else{
    		return true;
    	}
    }
    </script>
    </head>
    
    <body>
    <form action="#" method="post" onsubmit="return check(this)">
    	<div>
    		<label for="name">Name:</label><br><input type="text" name="name" id="name"><br>
    		<label for="email">Email:</label><br><input type="text" name="email" id="email"><br>
    		<label for="phone">Phone:</label><br><input type="text" name="phone" id="phone"><br>
    		<input type="submit"><input type="reset">
    	</div>
    </form>
    </body>
    </html>
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    New Coder
    Join Date
    Jan 2006
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thankyou - the script is in a page that is part of a web directory that uses info from various form input fields.

    If I replace the head code with the one you have supplied and the body code with the ones you have supplied, will the web directory still function correctly and call on the supplied info as it should.

    kind regards

  • #4
    New Coder
    Join Date
    Jan 2006
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm not really in a position to change the script completely as it will have too big a knock on effect so I just need to alter the existing code.

    thanks to anyone who can help.

    kind regards


  •  

    Posting Permissions

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