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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Could I have some regexp help? :)

    I was wondering if anyone could help me with regexp:

    1)I would like it so that both a first and last name can be entered and that both ' and - can be included
    2)I would like it so that addresses have to start with a number and then each word afterwards has to start with a capital letter and can include full stops, ' and -

    Any help is appreciated!

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    Code:
    var nameRE = /^[A-Z]([a=z]|[\'\-][A-Z])*[a-z](\s[A-Z]([a=z]|[\'\-][A-Z])?$/;
    You didn't ask for it, but...

    That insists that the first and last name start with an upper case letter and otherwise consist of only lower case letters, EXCEPT that if ' or - is included the letter after the ' or - must also be upper case.

    So:
    John O'Brien works
    Mallory Spence-Waite works
    JD Powers does NOT work
    Bob McFarlane does NOT work (oops! do we need to handle this, too?)
    April White-O does NOT work (can't end with upper case letter)

    If you don't care about being so fancy with upper/lower case:
    Code:
    var nameRE = /^[a-z][a=z\'\-]*[a-z](\s[a-z][a=z\'\-]*[a-z])?$/i; // i means ignore case
    ************
    Code:
    var addrRE = /^\d+(\s[A-Z][a-z\'\-\.]*[a-z\.])+$/
    Again, I modified your requirements slightly. That allows ' or - or . anywhere in a "word" but only allows the period at the end of the word.

    123 Fourth Ave. is okay
    123 W. Whistle-stop is okay
    777 Jone's Rd. is okay
    Jackson And Pine is NOT okay (I think it should be)
    Box 666 is NOT okay (I think it should be)
    1 Saints' Street is NOT okay

    You will have to decide if you want the rules to be that restrictive, in actuality.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    By the way, my own address would fail your test.

    7713 SE 199th Ave.

    Here, avenues are always PREFIXED with the N,E,S,W,NE,SE,SW,NW directional indicator.

    Streets have the directional indicator on the end:

    441 20th St. NE

    But in any case, streets and avenues are overwhelmingly numbered, not named, which kills your system completely. And the directional indicator is always capitalized, even when it is two letters.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New Coder
    Join Date
    Mar 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    By the way, my own address would fail your test.

    7713 SE 199th Ave.

    Here, avenues are always PREFIXED with the N,E,S,W,NE,SE,SW,NW directional indicator.

    Streets have the directional indicator on the end:

    441 20th St. NE

    But in any case, streets and avenues are overwhelmingly numbered, not named, which kills your system completely. And the directional indicator is always capitalized, even when it is two letters.
    Ah, I live in England so we don't have directional indicators for our streets, so hopefully that solves the issue

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    And you never have numbered streets, either??

    123 25th Street

    ???
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    New Coder
    Join Date
    Mar 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Code:
    var nameRE = /^[A-Z]([a=z]|[\'\-][A-Z])*[a-z](\s[A-Z]([a=z]|[\'\-][A-Z])?$/;
    You didn't ask for it, but...

    That insists that the first and last name start with an upper case letter and otherwise consist of only lower case letters, EXCEPT that if ' or - is included the letter after the ' or - must also be upper case.

    So:
    John O'Brien works
    Mallory Spence-Waite works
    JD Powers does NOT work
    Bob McFarlane does NOT work (oops! do we need to handle this, too?)
    April White-O does NOT work (can't end with upper case letter)

    If you don't care about being so fancy with upper/lower case:
    Code:
    var nameRE = /^[a-z][a=z\'\-]*[a-z](\s[a-z][a=z\'\-]*[a-z])?$/i; // i means ignore case
    ************
    Code:
    var addrRE = /^\d+(\s[A-Z][a-z\'\-\.]*[a-z\.])+$/
    Again, I modified your requirements slightly. That allows ' or - or . anywhere in a "word" but only allows the period at the end of the word.

    123 Fourth Ave. is okay
    123 W. Whistle-stop is okay
    777 Jone's Rd. is okay
    Jackson And Pine is NOT okay (I think it should be)
    Box 666 is NOT okay (I think it should be)
    1 Saints' Street is NOT okay

    You will have to decide if you want the rules to be that restrictive, in actuality.
    For the address coding, I am trying something like this:

    Code:
    function address2(address)
    {           
    	var reg=/^\d+(\s[A-Z][a-z\'\-\.]*[a-z\.])+$/;
    	var x=document.forms["customer"]["compaddress"].value;
    	if (reg.test(x)===false)
    	{
    	            alert("You have not entered a valid address in the Company Address field. Enter in a suitable value (i.e. in a 123 Real Street format)");
    	}
    }
    And calling it like this:

    Code:
    		else if (address2(address)===false)
    	{
    		return address2(address);
    		return false;
    	}
    }
    However, when I try something such as Box 666, it doesn't display the test alert, suggesting that I have implemented it wrong :/

    Is there any way to fix this?

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    Code makes no sense.

    You pass a value to the function when you call it:
    Code:
        if (address2(address)===false)
    but then in the function you never use the passed argument and *ALWAYS* go check
    Code:
        document.forms["customer"]["compaddress"].value;
    So if the value in "compaddress" is okay, you indeed won't get any alert.

    On top of that, you do
    Code:
       		return address2(address);
    		return false;
    The first return means the second one is unnecessary. return means "return from the function RIGHT NOW and do not execute ANY more code in the function."

    So... I can't really tell why/how this is/isn't working without seeing the <form> in question. But in any case, either use the address argument to the function or don't have an argument, at all.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Try this:-

    Code:
    <script type = "text/javascript">
    
    var str1 = "jEAn-paul o'flaNAGan-macDONald";
    str = str.toLowerCase().replace(/\b[a-z]/g,function(w){return w.toUpperCase()});
    alert(str); 
    
    </script>
    My own house has a name, not a number. I don't know of any UK addresses like 123 56th Street, but it may be possible. Roads/streets/avenues are normally named. I would not impose too strict requirements on your users to comply with a particular format. They ought to know their own address. If you want the words capitalised, use the above script rather than reject an entry because it is not properly capitalised. That just annoys your users.

    Here is a full validation for First Name and Last Name (easily changed if you want first and last names in a single field):-

    Code:
    <html>
    <head>
    
    <script type = "text/javascript">
    
    function check(form) {
    var fn = form.firstname;
    var fnv = form.firstname.value;
    fnv = fnv.replace(/\s+/g,"");  // strip all spaces OR
    //fnv = fnv.replace(/^\s+|\s+$/g,"");  // strip only leading and trailing spaces;
    //fnv = fnv.replace(/\s{2,}/g," ");  // Replace multiple spaces with one space
    // convert whatever is entered into lower case but with a capitalised first letter
    fnv = fnv.toLowerCase().replace(/\b[a-z]/g,function(w){return w.toUpperCase()});
    form.firstname.value = fnv;  // write it back to the field
    if ((fnv == "Firstname") || (fnv.length < 2) || (/[^a-z\-\']/gi.test(fnv))) {  // only a-z hyphen and apostophe allowed in proper name
    form.firstname.value= "";  // clear the invalid field
    alert("Invalid characters or name too short!  Please enter your first name!");
    myfield = fn;  // note myfield must be a global variable
    setTimeout('myfield.focus();myfield.select();' , 10);  // to overcome bug in Firefox
    return false;
    }
    
    var ln = form.lastname;
    var lnv = form.lastname.value;
    lnv = lnv.replace(/\s+/g,"");  // strip all spaces OR
    //lnv = lnv.replace(/^\s+|\s+$/g,"");  // strip only leading and trailing spaces;
    //lnv = lnv.replace(/\s{2,}/g," ");  // Replace multiple spaces with one space
    // convert whatever is entered into lower case but with a capitalised first letter
    lnv = lnv.toLowerCase().replace(/\b[a-z]/g,function(w){return w.toUpperCase()});
    form.lastname.value = lnv;  // write it back to the field
    if ((lnv == "Lastname") || (lnv.length < 3) || (/[^a-z\-\']/gi.test(lnv))) {  // only a-z hyphen and apostophe allowed in proper name
    form.lastname.value= "";  // clear the invalid field
    alert("Invalid characters or name too short! Please enter your last name!");
    myfield = ln;  // note myfield must be a global variable
    setTimeout('myfield.focus();myfield.select();' , 10);  // to overcome bug in Firefox
    return false;
    }
    
    return true;
    }
    
    </script>
    </head>
    <body>
    
    <form name = "form" action="" onsubmit="return check(this)">
    <input id="firstname" name="firstname" type="text" value="First Name" onfocus="this.value==this.defaultValue?this.value='':null" onblur="this.value==''?this.value=this.defaultValue:null">
    <input id="lastname" name="lastname" type="text" value="Last Name" onfocus="this.value==this.defaultValue?this.value='':null" onblur="this.value==''?this.value=this.defaultValue:null">
    <input type="submit" value="Submit">
    </form>
    
    </body>
    </html>
    Last edited by Philip M; 03-19-2012 at 11:01 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    Philip: Wasn't it you who noted that some Scottish names need inner capitalization and some don't?

    e.g., MacIntosh vs. Macadam? (I don't think those actual examples are correct, but I'm sure you can correct me.)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Old Pedant View Post
    Philip: Wasn't it you who noted that some Scottish names need inner capitalization and some don't?

    e.g., MacIntosh vs. Macadam? (I don't think those actual examples are correct, but I'm sure you can correct me.)
    I understand that the prefix Mac meaning "son of" requires the following proper name to be capitalised - MacDonald, but where the prefix Mac is followed by a person's occupation, e.g. Macnab (son of the abbott), Maccosh (son of the footman), or Mackenzie (son of the fair one) the part after Mac is not capitalised.

    Help Capitalizing Scottish Names

    If you want to cope with this, you must use one of the suggested scripts in that thread. Or perhaps best to just ignore it. MacDonald is in fact often spelled Macdonald. But the hamburger chain is of course McDonalds.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #11
    New Coder
    Join Date
    Mar 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Code makes no sense.

    You pass a value to the function when you call it:
    Code:
        if (address2(address)===false)
    but then in the function you never use the passed argument and *ALWAYS* go check
    Code:
        document.forms["customer"]["compaddress"].value;
    So if the value in "compaddress" is okay, you indeed won't get any alert.

    On top of that, you do
    Code:
       		return address2(address);
    		return false;
    The first return means the second one is unnecessary. return means "return from the function RIGHT NOW and do not execute ANY more code in the function."

    So... I can't really tell why/how this is/isn't working without seeing the <form> in question. But in any case, either use the address argument to the function or don't have an argument, at all.
    My form for this is as follows:

    Code:
     <form name="customer" action="mailto:apd15@hotmail.co.uk" method="post" onsubmit="return ValidateCustomerDetails();">
     <p><b><u>Customer Details Form</b></u></p>
     Full Name:<input type="text" name="custname"><br>
     Company Name:<input type="text" name="compname"><br>
     Company Address:<textarea cols="30" rows="5" name="compaddress"></textarea><br>
     Customer E-Mail Address:<input type="text" name="custemailaddress"><br>
     Company Phone Number:<input type="text" name="compphonenumber"><br>
     </form>
    And my JavaScript function that I am trying to apply to it is the following:

    Code:
    function ValidateCustomers()
    {
    	var name=document.forms["customer"]["custname"].value;
    	var comp=document.forms["customer"]["compname"].value;
    	var address=document.forms["customer"]["compaddress"].value;
    	var email=document.forms["customer"]["custemailaddress"].value;
    	var phone=document.forms["customer"]["compphonenumber"].value;
    	if (((((hasWhiteSpace(name))&&(hasWhiteSpace(comp))&&(hasWhiteSpace(address))&&(hasWhiteSpace2(email))&&(hasWhiteSpace(phone))))))
    	{
    	    alert("You have not entered any suitable values for the Full Name, Company Name, Company Address, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
    	    return false;
    	}
    	else if (((((name===null||name==="")&&(comp===null||comp==="")&&(address===null||address==="")&&(email===null||email==="")&&(phone===null||phone==="")))))
    	{
    		alert("You have not entered any suitable values for the Full Name, Company Name, Company Address, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values");
    	    return false;
    	}
    	else if ((((hasWhiteSpace(name))&&(hasWhiteSpace(comp))&&(hasWhiteSpace(address))&&(hasWhiteSpace2(email)))))
    	{
    		  alert("You have not entered any suitable values for the Full Name, Company Name, Company Address or Customer E-Mail Address fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
    	      return false;
    	}
    	else if ((((name===null||name==="")&&(comp===null||comp==="")&&(address===null||address==="")&&(email===null||email===""))))
    	{
    		alert("You have not entered any suitable values for the Full Name, Company Name, Company Address or Customer E-Mail Address fields. Enter in suitable values");
    	    return false;
    	}
        else if ((((hasWhiteSpace(name))&&(hasWhiteSpace(comp))&&(hasWhiteSpace(address))&&(hasWhiteSpace(phone)))))
    	{
    		  alert("You have not entered any suitable values for the Full Name, Company Name, Company Address or Company Phone Number fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
    	      return false;
    	}
    	else if ((((name===null||name==="")&&(comp===null||comp==="")&&(address===null||address==="")&&(phone===null||phone===""))))
    	{
    		alert("You have not entered any suitable values for the Full Name, Company Name, Company Address or Company Phone Number fields. Enter in suitable values");
    	    return false;
    	}
    	else if ((((hasWhiteSpace(name))&&(hasWhiteSpace(comp))&&(hasWhiteSpace2(email))&&(hasWhiteSpace(phone)))))
    	{
    		  alert("You have not entered any suitable values for the Full Name, Company Name, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
    	      return false;
    	}
    	else if ((((name===null||name==="")&&(comp===null||comp==="")&&(email===null||email==="")&&(phone===null||phone===""))))
    	{
    		alert("You have not entered any suitable values for the Full Name, Company Name, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values");
    	    return false;
    	}
    	else if ((((hasWhiteSpace(name))&&(hasWhiteSpace(address))&&(hasWhiteSpace2(email))&&(hasWhiteSpace(phone)))))
    	{
    		  alert("You have not entered any suitable values for the Full Name, Company Address, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
    	      return false;
    	}
    	else if ((((name===null||name==="")&&(address===null||address==="")&&(email===null||email==="")&&(phone===null||phone===""))))
    	{
    		alert("You have not entered any suitable values for the Full Name, Company Address, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values");
    	    return false;
    	}
    	else if ((((hasWhiteSpace(name))&&(hasWhiteSpace(address))&&(hasWhiteSpace2(email))&&(hasWhiteSpace(phone)))))
    	{
    		  alert("You have not entered any suitable values for the Full Name, Company Address, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
    	      return false;
    	}
    	else if ((((name===null||name==="")&&(address===null||address==="")&&(email===null||email==="")&&(phone===null||phone===""))))
    	{
    		alert("You have not entered any suitable values for the Full Name, Company Address, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values");
    	    return false;
    	}
    	else if ((((hasWhiteSpace(name))&&(hasWhiteSpace(address))&&(hasWhiteSpace2(email))&&(hasWhiteSpace(phone)))))
    	{
    		  alert("You have not entered any suitable values for the Full Name, Company Address, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
    	      return false;
    	}
    	else if ((((name===null||name==="")&&(address===null||address==="")&&(email===null||email==="")&&(phone===null||phone===""))))
    	{
    		alert("You have not entered any suitable values for the Full Name, Company Address, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values");
    	    return false;
    	}
    	else if ((((hasWhiteSpace(comp))&&(hasWhiteSpace(address))&&(hasWhiteSpace2(email))&&(hasWhiteSpace(phone)))))
    	{
    		  alert("You have not entered any suitable values for the Company Name, Company Address, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
    	      return false;
    	}
    	else if ((((comp===null||comp==="")&&(address===null||address==="")&&(email===null||email==="")&&(phone===null||phone===""))))
    	{
    		alert("You have not entered any suitable values for the Company Name, Company Address, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values");
    	    return false;
    	}
    	else if (((hasWhiteSpace(name))&&(hasWhiteSpace(comp))&&(hasWhiteSpace(address))))
    	{
    		  alert("You have not entered any suitable values for the Full Name, Company Name or Company Address fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
    	      return false;
    	}
    	else if (((name===null||name==="")&&(comp===null||comp==="")&&(address===null||address==="")))
    	{
    		alert("You have not entered any suitable values for the Full Name, Company Name or Company Address fields. Enter in suitable values");
    	    return false;
    	}
    	else if (((hasWhiteSpace(name))&&(hasWhiteSpace(comp))&&(hasWhiteSpace2(email))))
    	{
    		  alert("You have not entered any suitable values for the Full Name, Company Name or Customer E-Mail Address fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
    	      return false;
    	}
    	else if (((name===null||name==="")&&(comp===null||comp==="")&&(email===null||email==="")))
    	{
    		alert("You have not entered any suitable values for the Full Name, Company Name or Customer E-Mail Address fields. Enter in suitable values");
    	    return false;
    	}
    	else if (((hasWhiteSpace(name))&&(hasWhiteSpace(comp))&&(hasWhiteSpace(phone))))
    	{
    		  alert("You have not entered any suitable values for the Full Name, Company Name or Company Phone Number fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
    	      return false;
    	}
    	else if (((name===null||name==="")&&(comp===null||comp==="")&&(phone===null||phone==="")))
    	{
    		alert("You have not entered any suitable values for the Full Name, Company Name or Company Phone Number fields. Enter in suitable values");
    	    return false;
    	}
    	else if (((hasWhiteSpace(name))&&(hasWhiteSpace(address))&&(hasWhiteSpace2(email))))
    	{
    		  alert("You have not entered any suitable values for the Full Name, Company Address or Customer E-Mail Address fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
    	      return false;
    	}
    	else if (((name===null||name==="")&&(address===null||address==="")&&(email===null||email==="")))
    	{
    		alert("You have not entered any suitable values for the Full Name, Company Address or Customer E-Mail Address fields. Enter in suitable values");
    	    return false;
    	}
    	else if (((hasWhiteSpace(name))&&(hasWhiteSpace(address))&&(hasWhiteSpace(phone))))
    	{
    		  alert("You have not entered any suitable values for the Full Name, Company Address or Company Phone Number fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
    	      return false;
    	}
    	else if (((name===null||name==="")&&(address===null||address==="")&&(phone===null||phone==="")))
    	{
    		alert("You have not entered any suitable values for the Full Name, Company Address or Company Phone Number fields. Enter in suitable values");
    	    return false;
    	}
    	else if (((hasWhiteSpace(name))&&(hasWhiteSpace2(email))&&(hasWhiteSpace(phone))))
    	{
    		  alert("You have not entered any suitable values for the Full Name, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
    	      return false;
    	}
    	else if (((name===null||name==="")&&(email===null||email==="")&&(phone===null||phone==="")))
    	{
    		alert("You have not entered any suitable values for the Full Name, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values");
    	    return false;
    	}
    	else if (((hasWhiteSpace(comp))&&(hasWhiteSpace(address))&&(hasWhiteSpace2(email))))
    	{
    		  alert("You have not entered any suitable values for the Company Name, Company Address or Customer E-Mail Address fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
    	      return false;
    	}
    	else if (((comp===null||comp==="")&&(address===null||address==="")&&(email===null||email==="")))
    	{
    		alert("You have not entered any suitable values for the Company Name, Company Address or Customer E-Mail Address fields. Enter in suitable values");
    	    return false;
    	}
    	else if (((hasWhiteSpace(comp))&&(hasWhiteSpace2(email))&&(hasWhiteSpace(phone))))
    	{
    		  alert("You have not entered any suitable values for the Company Name, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
    	      return false;
    	}
    	else if (((comp===null||comp==="")&&(email===null||email==="")&&(phone===null||phone==="")))
    	{
    		alert("You have not entered any suitable values for the Company Name, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values");
    	    return false;
    	}
    	else if (((hasWhiteSpace(address))&&(hasWhiteSpace2(email))&&(hasWhiteSpace2(phone))))
    	{
    		  alert("You have not entered any suitable values for the Company Address, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
    	      return false;
    	}
    	else if (((comp===null||comp==="")&&(address===null||address==="")&&(email===null||email==="")))
    	{
    		alert("You have not entered any suitable values for the Company Name, Customer E-Mail Address or Company Phone Number fields. Enter in suitable values");
    	    return false;
    	}
    	else if ((hasWhiteSpace(name))&&(hasWhiteSpace(comp)))
    	{
    		  alert("You have not entered any suitable values for the Full Name or Company Name fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
    	      return false;
    	}
    	else if ((name===null||name==="")&&(comp===null||comp===""))
    		{
    		alert("You have not entered any suitable values for the Full Name or Company Name fields. Enter in suitable values");
    	      return false;
    	}
    	else if ((hasWhiteSpace(name))&&(hasWhiteSpace(address)))
    	{
    		  alert("You have not entered any suitable values for the Full Name or Company Address fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
    	      return false;
    	}
    	else if ((name===null||name==="")&&(address===null||address===""))
    		{
    		alert("You have not entered any suitable values for the Full Name or Company Address fields. Enter in suitable values");
    	      return false;
    	}
    	else if ((hasWhiteSpace(name))&&(hasWhiteSpace2(email)))
    	{
    		  alert("You have not entered any suitable values for the Full Name or Customer E-Mail Address fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
    	      return false;
    	}
    	else if ((name===null||name==="")&&(email===null||email===""))
    		{
    		alert("You have not entered any suitable values for the Full Name or Customer E-Mail Address fields. Enter in suitable values");
    	      return false;
    	}
    	else if ((hasWhiteSpace(name))&&(hasWhiteSpace(phone)))
    	{
    		  alert("You have not entered any suitable values for the Full Name or Company Phone Number fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
    	      return false;
    	}
    	else if ((name===null||name==="")&&(phone===null||phone===""))
    		{
    		alert("You have not entered any suitable values for the Full Name or Company Phone Number fields. Enter in suitable values");
    	      return false;
    	}
    	else if ((hasWhiteSpace(comp))&&(hasWhiteSpace(address)))
    	{
    		  alert("You have not entered any suitable values for the Company Name or Company Address fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
    	      return false;
    	}
    	else if ((comp===null||comp==="")&&(address===null||address===""))
    		{
    		alert("You have not entered any suitable values for the Company Name or Company Address fields. Enter in suitable values");
    	      return false;
    	}
    	else if ((hasWhiteSpace(comp))&&(hasWhiteSpace2(email)))
    	{
    		  alert("You have not entered any suitable values for the Company Name or Customer E-Mail Address fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
    	      return false;
    	}
    	else if ((comp===null||comp==="")&&(email===null||email===""))
    		{
    		alert("You have not entered any suitable values for the Company Name or Customer E-Mail Address fields. Enter in suitable values");
    	      return false;
    	}
        else if ((hasWhiteSpace(comp))&&(hasWhiteSpace(phone)))
    	{
    		  alert("You have not entered any suitable values for the Company Name or Company Phone Number fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
    	      return false;
    	}
    	else if ((comp===null||comp==="")&&(phone===null||phone===""))
    		{
    		alert("You have not entered any suitable values for the Company Name or Company Phone Number fields. Enter in suitable values");
    	      return false;
    	}
    	else if ((hasWhiteSpace(address))&&(hasWhiteSpace2(email)))
    	{
    		  alert("You have not entered any suitable values for the Company Address or Customer E-Mail Address fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
    	      return false;
    	}
    	else if ((address===null||address==="")&&(email===null||email===""))
    		{
    		alert("You have not entered any suitable values for the Company Address or Customer E-Mail Address fields. Enter in suitable values");
    	      return false;
    	}
    	else if ((hasWhiteSpace(address))&&(hasWhiteSpace(phone)))
    	{
    		  alert("You have not entered any suitable values for the Company Address or Company Phone Number fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
    	      return false;
    	}
    	else if ((address===null||address==="")&&(phone===null||phone===""))
    		{
    		alert("You have not entered any suitable values for the Company Address or Company Phone Number fields. Enter in suitable values");
    	      return false;
    	}
    	else if ((hasWhiteSpace2(email))&&(hasWhiteSpace(phone)))
    	{
    		  alert("You have not entered any suitable values for the Customer E-Mail Address or Company Phone Number fields. Enter in suitable values, possibly by removing any leading or trailing spaces around the text");
    	      return false;
    	}
    	else if ((email===null||email==="")&&(phone===null||phone===""))
    		{
    		alert("You have not entered any suitable values for the Customer E-Mail Address or Company Phone Number fields. Enter in suitable values");
    	      return false;
    	}
    	else if (hasWhiteSpace(name))
    	{
    		alert("You have not entered a suitable value for the Full Name field. Enter in a suitable value, possibly by removing any leading or trailing spaces around the text");
    	    return false;
    	}
    	else if (name===null||name==="")
    	{
    				alert("You have not entered a suitable value for the Full Name field. Enter in a suitable value");
    				return false;
    	}
    	else if (hasWhiteSpace(comp))
    	{
    		alert("You have not entered a suitable value for the Company Name field. Enter in a suitable value, possibly by removing any leading or trailing spaces around the text");
    	    return false;
    	}
    	else if (comp===null||comp==="")
    	{
    				alert("You have not entered a suitable value for the Company Name field. Enter in a suitable value");
    				return false;
    	}
    etc
    	else if (email2(email)===false)
    	{
    		return email2(email);
    		return false;
    	}
    	else if (phone2(phone)===false)
    	{
    		return phone2(phone);
    		return false;
    	}
    		else if (name2(name)===false)
    	{
    		return name2(name);
    		return false;
    	}
    		else if (address2(address)===false)
    	{
    		return address2(address);
    		return false;
    	}
    }
    It's essentially a lot of different branches for validation

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    The repetition should alert you to the fact that your code is very poorly written. I would advise that you validate each field separately (one at a time), with error messages relevant to that specific field and that specific error. You should strip leading and trailing spaces (and multiple consecutive spaces) in your script automatically as a matter of course, not complain about them to the user. The value of a form field can never be null, only "" (blank). Why are you using the === strict comparison operator?

    Form validation of the pattern if (document.forms[0].elements[1].value == "") is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. Numeric values, such as zip codes and phone numbers, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.
    Last edited by Philip M; 03-19-2012 at 11:55 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #13
    New Coder
    Join Date
    Mar 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    The repetition should alert you to the fact that your code is poorly written. I would advise that you validate each field separately (one at a time), with error messages relevant to that field and that specific error.

    Form validation of the pattern if (document.forms[0].elements[1].value == "") is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. Numeric values, such as zip codes and phone numbers, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.
    I'm still struggling to see why the coding you gave me doesn't seem to work :/

    I'm not going to have any validation for the company name, since companies can have strange names

  • #14
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    All the code that I post works for me.

    I notice that your form action is - action="mailto:apd15@hotmail.co.uk" This is a complete no-no.

    The trouble with using this long-obsolete method (mailto) to send form results is its unpredictability. The method it is highly dependent on the browser in use and the email client in use (some people have only Yahoo, Gmail or Hotmail). In particular, your visitor must have Outlook or Outlook Express as the default client for this to work correctly. Even if your visitor is using Internet Explorer, but the default mail client is different (e.g. Eudora or Thunderbird), your mailto form will not work. With all of the browser troubles, you're likely to lose about half of your users' messages. Most of the email clients that can successfully send a mail will prompt the user with a somewhat threatening security dialog prior to sending - this can scare many users from continuing. Other users will not wish to reveal their email address. Also, what about people with Javascript disabled?

    In addition, if you place an unobfuscated email address in your webpage, the bots will quickly find it and inundate you in spam.

    Modern browsers no longer accept mailto: as a form action - they simply open the email program (if any) and ignore the form. If you are going to use a form then use a server-side CGI formmail script as the action - there are several good free ones out there.

    I do not wish to be thought unkind, but it seems to me that you simply do not know enough about Javascript to do what you intend. The fault, dear Brutus, lies not in the stars ...
    Last edited by Philip M; 03-20-2012 at 12:06 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #15
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    Oh my lord, what a MESS!

    First of all, the .value property of a <form> field is *NEVER* null. So all your ==null checks are useless. (The .value property may of course be a blank string "", but that is *NOT* a null.)

    And if you use a regular expression to validate, you don't need to make the silly hasWhiteSpace checks.

    And if you check each field separately and BUILD UP the error message, then you don't need all that complex set of multiple if conditions.

    But having said all that, the biggest problem you face is here:
    Code:
    <form ... action="mailto:apd15@hotmail.co.uk" ...>
    Roughly a third to a half of all typical users will be unable to use your <form>. "mailto" only works if the user's browser is properly set up to indeed send <form> data to an email program. Which means that a user using a browser other than the default one won't be set up properly unless he/she understands the often arcane ways to specify an email client to non-default (e.g, in most cases non-IE) browsers.

    You *REALLY* should instead be sending the <form> to some server-side code (PHP, CGI, ASP, etc.) that will take the form <post> and send the email for you, so that you aren't depending upon the user's ability to link his/her browser to the email client.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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