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 8 of 8

Thread: Form validation

  1. #1
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Form validation

    Hi,
    I am trying to make a form check two fields (using regular expressions) and then submit.

    So far I have created my expressions but i cant get the form to validate correctly! I seem to get the error associated with the loyalty card rather than the postcode check.
    Any help would be appreciated

    My code is:

    <script type="text/javascript">

    function validate(postalCode) {
    var re = new RegExp(/^([a-zA-Z]{2}[0-9]{1,2})|([0-9]{2}[a-zA-Z]{1,2})$/);
    var str = postalCode.value;
    if (re.test(str))
    return true;
    else
    {
    alert("That is not a valid postcode. Please verify your input.");
    return false;
    }
    }
    function validate(cardNumber)
    {
    var re = new RegExp(/^([a-zA-Z]{1}[a-zA-Z0-9]{4})([a-zA-Z0-9]{5})([a-zA-Z0-9]{3})([0-9]{1}[!&@?]{1})$/);
    var str = cardNumber.value;
    if (re.test(str))
    return true;
    else
    {
    alert("That is not a valid card number. Please verify your input.");
    return false;
    }
    }
    </script>

    <html>
    <form id="order" action= "" method="post" >
    <table cellpadding="5">

    <tr>
    <td>Postal Code</td>

    <td><input type="text" name="postalCode" maxlength=
    "30" /></td>
    </tr>
    <tr>
    <td>Loyalty card</td>
    <td><input type="text" name="cardNumber" maxlength=
    "30" /></td>
    </tr>


    <input id = "submit" type="submit" value="Submit" onclick="validate(postalCode); validate(cardNumber)" />

    </td>
    </form>

    </html>

  2. #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Ermmmmmm... both your functions have the same name, so only the first one will be called.

  3. #3
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry im a newbie!! Could you help me out?

  4. #4
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Change your function names to
    Code:
    function validatePC(postalCode)
    // and
    function validateCard(postalCode)
    and your html:

    Code:
    <input id = "submit" type="submit" value="Submit" onclick="validatePC(postalCode); validateCard(cardNumber)" />

  5. #5
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you so much!!! Very much appreciated!!!
    Sorry - i'm a beginner!

  6. #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    19,551
    Thanks
    221
    Thanked 2,712 Times in 2,686 Posts
    Your postcode validation regex is incorrect as it requires two letters in the outcode. Several places have only one outcode letter - B, G, L, M, S as well as London E and W. The letters must be capitals, not lower case.

    The incode is also incorrect - the format is digit-letter-letter.

    So your regex should be:-

    Code:
    var re = new RegExp(/^([A-Z]{1,2}[0-9]{1,2})\s([0-9][A-Z]{2})$/);
    Of course, not all postcodes which pass this text are valid. Letters C, I, K, M, O and V are never used in the incode. So GU12 4MV is not a valid postcode.

  7. #7
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for all your help!
    How would i alter the code above so it first validates the post code and card number and then checks to see if the given postcode is in a certain area (e.g. users in CA1) so only these users would get a different message?

  8. #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    19,551
    Thanks
    221
    Thanked 2,712 Times in 2,686 Posts
    Code:
    var str = postalCode.value; 
    if (str.substr(0,3) == "CA1") {
    alert ("Your postcode is CA1 and you live in Carlisle")
    }
    if (str.substr(0,4) == "GU34") {
    alert ("Your postcode is GU234 and you live in Alton")
    }
    Last edited by Philip M; Apr 17th, 2011 at 06:34 PM.


 

Posting Permissions

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