www.webdeveloper.com
Results 1 to 8 of 8

Thread: [RESOLVED] form validation

  1. #1
    Join Date
    Nov 2010
    Location
    Bristol, United Kingdom
    Posts
    47

    resolved [RESOLVED] form validation

    Hi people, me again. sorry!

    I have two different criteria for two seperate form feilds both using regular expressions, however for some reason the second one is the only one which is applied. The code ignors the first rule no matter which way round they go. could someone please rewrite for me?

    here is my JS:

    Code:
    <script language="JavaScript" type="text/javascript">
    
    //<![CDATA[
    		   
    var regExpressFidelity = /^[a-zA-Z]{1}[a-zA-Z0-9]{4}\s*[a-zA-Z0-9]{5}\s*[a-zA-Z0-9]{3}\d{1}(@|!|\&){1}$/;
     
    function checkForm(aForm) {
    
            if (!regExpressFidelity.test(aForm.fidelityCardNumber.value)){
            window.alert("Invalid Card Number. Please check your number and try again.")
    
            aForm.fidelityCardNumber.focus()
            aForm.fidelityCardNumber.select()
            return false
                    }
            return true
            }
    
    
    var regExpressPostcode = /^([a-zA-Z]){2}([0-9][0-9]|[0-9]){1}([ ])([0-9][a-zA-z][a-zA-z]|[0-9][0-9][a-zA-z][a-zA-z]){1}$/;
    
    function checkForm(aForm) {
    
    method 'test()' */
            if (!regExpressPostcode.test(aForm.shipPostalCode.value)){
            window.alert("Please enter a correctly formatted UK postcode: eg. AB12 2BA or AB12 12AB")
    
            aForm.shipPostalCode.focus()
            aForm.shipPostalCode.select()
            return false
                    }
            return true
            }
    	//]]>	
    </script>
    Freelance Web Design/Developer - www.mikeoram.co.uk

  2. #2
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    method 'test()' */
    You forgot the opening /* for this.
    |||||
    o . Q
    ___ "You live and you learn; or you don't live long." - Lazarus Long

    ^_^

  3. #3
    Join Date
    Jan 2005
    Posts
    348
    You can't have two function's with the same name.
    Might also be useful to see where/how you are trying to call them.

    Also, your RegExp for a UK Postcode doesn't look right - the first group is one or two letters, followed by one or two digits... Probably worth having a look on the net for a pre-built expression, as some of the rules can be pretty subtle.

  4. #4
    Join Date
    Nov 2010
    Location
    Bristol, United Kingdom
    Posts
    47
    Quote Originally Posted by WolfShade View Post
    method 'test()' */
    You forgot the opening /* for this.
    opps sorry that was a comment which i tried to remove from my code when pasting it into the forum. here is the code with comments removed correctly:

    <script language="JavaScript" type="text/javascript">

    //<![CDATA[

    var regExpressFidelity = /^[a-zA-Z]{1}[a-zA-Z0-9]{4}\s*[a-zA-Z0-9]{5}\s*[a-zA-Z0-9]{3}\d{1}(@|!|\&){1}$/;

    function checkForm(aForm) {

    if (!regExpressFidelity.test(aForm.fidelityCardNumber.value)){
    window.alert("Invalid Card Number. Please check your number and try again.")

    aForm.fidelityCardNumber.focus()
    aForm.fidelityCardNumber.select()
    return false
    }
    return true
    }


    var regExpressPostcode = /^([a-zA-Z]){2}([0-9][0-9]|[0-9]){1}([ ])([0-9][a-zA-z][a-zA-z]|[0-9][0-9][a-zA-z][a-zA-z]){1}$/;

    function checkForm(aForm) {

    if (!regExpressPostcode.test(aForm.shipPostalCode.value)){
    window.alert("Please enter a correctly formatted UK postcode: eg. AB12 2BA or AB12 12AB")

    aForm.shipPostalCode.focus()
    aForm.shipPostalCode.select()
    return false
    }
    return true
    }
    //]]>
    </script>
    Freelance Web Design/Developer - www.mikeoram.co.uk

  5. #5
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    And omnicity picked out the one thing that is going to kill the validation from the start - you canNOT have two functions with the same name in the same document (either typed in or refereced by src.) That's the main thing, right there. Conflict. One gets ignored; assuming it doesn't flat out break.
    |||||
    o . Q
    ___ "You live and you learn; or you don't live long." - Lazarus Long

    ^_^

  6. #6
    Join Date
    Nov 2010
    Location
    Bristol, United Kingdom
    Posts
    47
    right ok i see so i need to combine the two together into one function? how would I go about doing this? my javascript knowledge is very patchy im afraid!



    Also, your RegExp for a UK Postcode doesn't look right - the first group is one or two letters, followed by one or two digits... Probably worth having a look on the net for a pre-built expression, as some of the rules can be pretty subtle.
    thanks for the heads up and i am aware that there are alot more rules which can be applied when validating the postcode, however for what i need it for this rule is all i need. i appreciate your help however.
    Freelance Web Design/Developer - www.mikeoram.co.uk

  7. #7
    Join Date
    Jan 2005
    Posts
    348
    Some of the London postcodes can be as simple as N1 1AA or similar - your RegExp only accepts ones with exactly two initial letters, but if you are sure that doesn't matter, then the simplest re-write gives you:

    Code:
    var regExpressFidelity = /^[a-zA-Z]{1}[a-zA-Z0-9]{4}\s*[a-zA-Z0-9]{5}\s*[a-zA-Z0-9]{3}\d{1}(@|!|\&){1}$/;
    var regExpressPostcode = /^([a-zA-Z]){2}([0-9][0-9]|[0-9]){1}([ ])([0-9][a-zA-z][a-zA-z]|[0-9][0-9][a-zA-z][a-zA-z]){1}$/;
    
    
    function checkForm(aForm) {
    
    if (!regExpressFidelity.test(aForm.fidelityCardNumber.value)){
    window.alert("Invalid Card Number. Please check your number and try again.")
    
    aForm.fidelityCardNumber.focus()
    aForm.fidelityCardNumber.select()
    return false
    }
    if (!regExpressPostcode.test(aForm.shipPostalCode.value)){
    window.alert("Please enter a correctly formatted UK postcode: eg. AB12 2BA or AB12 12AB")
    
    aForm.shipPostalCode.focus()
    aForm.shipPostalCode.select()
    return false
    }
    return true
    }

  8. #8
    Join Date
    Nov 2010
    Location
    Bristol, United Kingdom
    Posts
    47

    resolved

    Brilliant thank you! yes i am aware that some postcodes wont validate as it is but this form is only going to be used by a select set of people in a certain area all of whom have postcodes which will validate. just want to prevent them from making mistakes really!

    your code does indeed work! thank you so much!

    Quote Originally Posted by omnicity View Post
    Some of the London postcodes can be as simple as N1 1AA or similar - your RegExp only accepts ones with exactly two initial letters, but if you are sure that doesn't matter, then the simplest re-write gives you:

    Code:
    var regExpressFidelity = /^[a-zA-Z]{1}[a-zA-Z0-9]{4}\s*[a-zA-Z0-9]{5}\s*[a-zA-Z0-9]{3}\d{1}(@|!|\&){1}$/;
    var regExpressPostcode = /^([a-zA-Z]){2}([0-9][0-9]|[0-9]){1}([ ])([0-9][a-zA-z][a-zA-z]|[0-9][0-9][a-zA-z][a-zA-z]){1}$/;
    
    
    function checkForm(aForm) {
    
    if (!regExpressFidelity.test(aForm.fidelityCardNumber.value)){
    window.alert("Invalid Card Number. Please check your number and try again.")
    
    aForm.fidelityCardNumber.focus()
    aForm.fidelityCardNumber.select()
    return false
    }
    if (!regExpressPostcode.test(aForm.shipPostalCode.value)){
    window.alert("Please enter a correctly formatted UK postcode: eg. AB12 2BA or AB12 12AB")
    
    aForm.shipPostalCode.focus()
    aForm.shipPostalCode.select()
    return false
    }
    return true
    }
    Freelance Web Design/Developer - www.mikeoram.co.uk

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