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 5 of 5
  1. #1
    New Coder
    Join Date
    Jul 2009
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    form validation check if email or phone number entered??

    HI Guys, not the greatest with javascript

    I have this form validation code

    Code:
    function check_email(mailstring)
    {
    	valid = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
    	for(i=0; i < mailstring.length ;i++)
    	{
    		if (valid.indexOf(mailstring.charAt(i))<0)
    		{
    		return (false);
    		}
    	}
    	if (document.images)
    	{
    		one = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
    		two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    		if (!mailstring.match(one) && mailstring.match(two))
    		{
    		return (-1);
    		}
    	}
    }
    
    function validate()
    {
    
    	if (document.mailpage.contact_name.value=="")
    	{
    		alert("Name not entered")
    		return false
    	}
    	if (document.mailpage.contact_address.value=="")
    	{
    		alert("Address not entered")
    		return false
    	}
    	if (document.mailpage.contact_otherinfo.value=="")
    	{
    		alert("Operational Details / Other Information was not entered")
    		return false
    	}
    	
    	if (!check_email(document.mailpage.contact_email.value))
    	{
    		alert("Invalid Email address")
    		return false
    	}
    	if (document.mailpage.contact_phone.value=="")
    	{
    		alert("Phone not entered")
    		return false
    	}
    	
    	
    	
    }
    at the moment its checks that someting has been enetered in all these fields,

    what would be the best way to check that the email or phone nummber had been entered?? will not send if nothing enetered, but will send if one or both have been enetered?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,906
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    This ought to move you forward:-

    Code:
    <script type = "text/javascript">
    
    function validate() {
    
    	if (document.mailpage.contact_name.value=="") {
    		alert("Name not entered");
    		return false
    	}
    	if (document.mailpage.contact_address.value=="") {
    		alert("Address not entered");
    		return false
    	}
    	if (document.mailpage.contact_otherinfo.value=="") {
    		alert("Operational Details / Other Information was not entered");
    		return false
    	}
    
    var f = 0;
    var em = document.mailpage.contact_email.value;
    
    if (em != "") {  // if em contains a value
    if (!(/^([a-z0-9])([\w\.\-\+])+([a-z0-9])\@(([\w\-]?)+\.)+([a-z]{2,4})$/i.test(em))) {  
    alert("Invalid Email address - please re-enter");
    document.mailpage.contact_email.value = "";
    document.mailpage.contact_email.focus();
    return false;
    }
    else {f++}  // an email address has been entered
    }
    
    var ph = document.mailpage.contact_phone.value;
    ph = ph.replace(/[^0-9\-/,"");  //strip non-numeric characters except hyphen
    if (ph != "") {  // if ph contains a value after stripping
    if (ph.length <  10) {  
    alert ("Phone number not valid - please re-enter");
    document.mailpage.contact_phone.value = "";
    document.mailpage.contact_phone.value.focus();
    return false
    }
    else {f++}  // a phone number has been entered
    }
    
    if (f==0) {
    alert ("You must enter either your email address or your phone number, or both");
    return false;
    }
    else {return true}	
    }
    
    </script>

    Note that validation of the type if document.mailpage.contact_name.value=="") { is barely worthy of the name, and is virtually worthless, as even a single space or a ? will return false (i.e. pass the validation). You should
    a) strip leading and trailing spaces
    b) remove inappropriate characters (a name can only be a-z hyphen apostrophe space)
    c) check for a minimum length (say at least 3 alpha characters)




    "In the beginner's mind there are many possibilities, but in the expert's mind there are few” - Shunryu Suzuki (Japanese Zen priest, ?-1971)
    Last edited by Philip M; 07-24-2009 at 10:59 AM. Reason: Typo

  • Users who have thanked Philip M for this post:

    richyrich38 (07-24-2009)

  • #3
    New Coder
    Join Date
    Jul 2009
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you for the help and advice.

    Im very new to this and always looking at was to improve my coding.

    sorry to be a pain, do you know any good links for me read on Good form validation,

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,906
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Form validation has been covered a zillion times in this forum.

  • #5
    New Coder
    Join Date
    Jul 2009
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Fixed now
    Last edited by richyrich38; 07-24-2009 at 03:12 PM. Reason: Fixed problem


  •  

    Tags for this Thread

    Posting Permissions

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