www.webdeveloper.com
Results 1 to 12 of 12

Thread: Validation

  1. #1
    Join Date
    Nov 2005
    Location
    Scotland, UK
    Posts
    149

    Validation (small bonus to anyone who can help!)

    I have a form which checks the user input is a valid UK post code, if ithe postcode isn't valid a Javascript pop-up appears. However, the post code is optional so if the input box is blank I would like like to bypass the validation.

    The form code is:
    Code:
    <input type="text" id="postcode" maxlength="13" size="13" style="margin-left: 10px;" alt="Enter test postcode" />

    The JavaScript in the head is:
    Code:
    function testPostCode () {
      var myPostCode = document.getElementById('postcode').value;
      
      if (checkPostCode (myPostCode)) {
        document.getElementById('postcode').value = checkPostCode (myPostCode)
        return flase;
      } 
      else {alert ("Postcode has invalid format")};
    }
    "checkPostCode" is the function that performs the actual validation on the post code, I don't think it's necessary to list it here as it is very large and I'd like to bypass it anyway if the field is blank.

    Thanks
    Last edited by bldstr; 01-24-2006 at 04:44 AM. Reason: Title change

  2. #2
    Join Date
    Dec 2005
    Location
    Washington, DC
    Posts
    228
    Code:
    function testPostCode () {
      var myPostCode = document.getElementById('postcode').value;
      if(myPostCode == '') return false;
      if (checkPostCode (myPostCode)) {
        document.getElementById('postcode').value = checkPostCode (myPostCode)
        return false;
      } 
      else {alert ("Postcode has invalid format")};
    }

  3. #3
    Join Date
    Nov 2005
    Location
    Scotland, UK
    Posts
    149
    Thank you, very much appreciated.

    I wonder if you could help me with the other thing I'm not sure of? This form goes to another page when the user clicks submit, how do I stop it forwarding if there are any validation errors?

    The form action is:

    Code:
    <form name="allForm" method="post" action="/SapphireWebModule/partnersearchforprojects">
    Thanks once again.

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    function validate(f){
    if(validate condition is not fulfiled){
    alert('something');
    return false
    }
    }

    <form onsubmit="return validate(this)">

  5. #5
    Join Date
    Nov 2005
    Location
    Scotland, UK
    Posts
    149
    Thanks for your response; however, I don't really understand JavaScript well enough to incorporate that into my existing script. I've been trying to work this out for the past few days, reading various forms and examples but I really don't get it.

    The bits I can't figure out is what the (f) and (this) should be in correspondence with my exiting script.

  6. #6
    Join Date
    Nov 2005
    Location
    Scotland, UK
    Posts
    149
    OK, I'm desperate here

    I will Paypal 5 to anyone who can take this document and have the postcode validation fully work. At the moment it detects a wrongly entered postcode but does not stop the document passing to the next page if there's an error, I'm sure it would be pretty simple for anyone here to fix.

    I know it's not a lot of money but it shouldn't be a big job and I can't afford any more really.

    If they could also help with date validation that would be great, just PM me...
    Last edited by bldstr; 01-24-2006 at 04:41 AM.

  7. #7
    Join Date
    Dec 2005
    Location
    Queen Creek, AZ
    Posts
    1,564
    in your form tag, make sure you have: onsubmit="return testPostCode()"

    where testPostCode is the name of the function that tests it, if that is all you test.

    if the function returns false, the form will stop where it is and not process any more.

  8. #8
    Join Date
    Nov 2005
    Location
    Scotland, UK
    Posts
    149
    Thank you.

    However, with the following code I recieve two pop up error messages, rather than just one, if the user entres an invalid postcode:

    Code:
    function testPostCode () {
      var myPostCode = document.getElementById('sitePostcode').value;
      if(myPostCode == '') return true;
      else if (checkPostCode (myPostCode)) {
        document.getElementById('sitePostcode').value = checkPostCode (myPostCode)
        return true;
      }
      else {alert ("Postcode has invalid format")};
      return false;
    }

  9. #9
    Join Date
    Dec 2005
    Location
    Queen Creek, AZ
    Posts
    1,564
    you are calling a function inside this one: checkPostCode (myPostCode)

    make sure one isn't coming from there.

    also by calling:
    document.getElementById('sitePostcode').value = checkPostCode (myPostCode)
    after the if statement, you are actually calling the same function twice

  10. #10
    Join Date
    Nov 2005
    Location
    Scotland, UK
    Posts
    149
    Turns out the fault was in the onclick events in the form code, I hadn't removed the line I replaced with onsubmit="return testPostCode()"

  11. #11
    Join Date
    Dec 2005
    Location
    Queen Creek, AZ
    Posts
    1,564
    All working then?

    I'll be here another 5 mins if you need anything else.

  12. #12
    Join Date
    Nov 2005
    Location
    Scotland, UK
    Posts
    149
    I've PMed you

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles