www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: [RESOLVED] Regular Expression

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

    resolved [RESOLVED] Regular Expression

    Hi Gurus!

    Iv written a regular expression to single out specific postcodes, however it currently returns all postcodes submitted. Could someone take a look at my regular expression and help me correct it?

    Code:
    /^[M]{1}[K]{1}([1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|17|19|77]){1,2}$/;
    I am trying to single out only postcodes which start with MK1 - MK15, MK17, MK19 and MK77

    Any ideas?

    Thanks for any help!

    Mike
    Freelance Web Design/Developer - www.mikeoram.co.uk

  2. #2
    Join Date
    Apr 2009
    Posts
    96
    mk(15|17|19|77|1)\s
    Last edited by Rufe0; 04-13-2011 at 05:01 AM.

  3. #3
    Join Date
    Nov 2010
    Location
    Bristol, United Kingdom
    Posts
    47
    sorry i need to include all possibilities between MK1 and MK15. Sorry this may not have been clear the first time around. If I use your code but include the additional possibilities from my expression will this work? is there a short hand way of including the numbers between 1 and 15?

    Thanks.
    Freelance Web Design/Developer - www.mikeoram.co.uk

  4. #4
    Join Date
    Nov 2010
    Location
    Bristol, United Kingdom
    Posts
    47
    iv tried your code using a postcode MK1 however it still displays the alert no matter what postcode is added. here is my full script:

    Code:
    var regExpressMilton = /^MK(1|15|17|19|77)$/;
     
    function checkForm(aForm) {
    				
    		if (!regExpressMilton.test(aForm.shipPostalCode.value)) {
    		window.alert("congratulations! You have been awarded a special gift with your order!")	
    		return true
    		}
    return true
    }
    Freelance Web Design/Developer - www.mikeoram.co.uk

  5. #5
    Join Date
    Apr 2009
    Posts
    96
    mk(1|2|3|4|5|6|7|8|9|10|11|12|13|15|17|19|77)\s

  6. #6
    Join Date
    Apr 2009
    Posts
    96
    HTML Code:
    <html>
    <body>
    
    <script type="text/javascript">
    var regexp1=new RegExp("mk(1|2|3|4|5|6|7|8|9|10|11|12|13|15|17|19|77)\s");
    
    document.write(regexp1.test("mk1 gh45"));
    document.write("<br />");
    document.write(regexp1.test("aa1 aa43"));
    </script>
    
    </body>
    </html>

  7. #7
    Join Date
    Nov 2010
    Location
    Bristol, United Kingdom
    Posts
    47
    ok iv tried this code and now it doesnt display my alert in any situation?

    here is my new code?

    Code:
    var regExpressMilton = /^MK(1|2|3|4|5|6|7|8|9|10|11|12|13|15|17|19|77)\s$/;
     
    function checkForm(aForm) {
    
    		if (regExpressMilton.test(aForm.shipPostalCode.value)) {
    		window.alert("congratulations! You have been awarded a special gift with your order!")	
    		return true
    		}
    	
    		
            return true
            }
    I see from your test that it seems to be working, so what is wrong with my code? really stuck on this! :s

    Thanks!
    Freelance Web Design/Developer - www.mikeoram.co.uk

  8. #8
    Join Date
    Apr 2009
    Posts
    96
    HTML Code:
    <html>
    <body>
    
    <script type="text/javascript">
    var regexp1=new RegExp("mk(1|2|3|4|5|6|7|8|9|10|11|12|13|15|17|19|77)[^(0-9)]");
    
    document.write(regexp1.test("mk1 gh45"));
    document.write("<br />");
    document.write(regexp1.test("aa1 aa43"));
    </script>
    
    </body>
    </html>

  9. #9
    Join Date
    Apr 2009
    Posts
    96
    try this
    HTML Code:
    var regExpressMilton = new RegExp("mk(1|2|3|4|5|6|7|8|9|10|11|12|13|15|17|19|77)[^(0-9)]");
    
    function checkForm(aForm) {
    	var a = regExpressMilton.test(aForm.shipPostalCode.value);
    		if (a) {
    		window.alert("congratulations! You have been awarded a special gift with your order!")	
    		return true
    		}
    	return true
    }

  10. #10
    Join Date
    Nov 2010
    Location
    Bristol, United Kingdom
    Posts
    47
    Iv tried this and it doesnt seem to be giving me an alert message? perhaps if I show you my full code you will better udnerstand why I used the method I have.

    Code:
    var regExpressPostcode = /^([a-zA-Z]){2}([0-9][0-9]|[0-9]){1}\s([0-9][a-zA-z][a-zA-z]|[0-9][0-9][a-zA-z][a-zA-z]){1}$/;
    		   
    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 regExpressMilton = /^MK(1|2|3|4|5|6|7|8|9|10|11|12|13|15|17|19|77)[^(0-9)]$/;
     
    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
                    }
    
    // check for a valid postcode address format using the RegExp object method 'test()' 
            if (!regExpressPostcode.test(aForm.shipPostalCode.value)){
            window.alert("Please enter a correctly formatted UK postcode: eg. AB12 2BA or AB12 12AB")
                    //gives focus to field and selects incorrect postcode format text
            aForm.shipPostalCode.focus()
            aForm.shipPostalCode.select()
            return false
                    }
    				
    		if (regExpressMilton.test(aForm.shipPostalCode.value)) {
    		window.alert("congratulations! You have been awarded a special gift with your order!")	
    		return true
    		}
    	
    
            return true
            }
    thanks,

    Mike
    Freelance Web Design/Developer - www.mikeoram.co.uk

  11. #11
    Join Date
    Nov 2010
    Location
    Bristol, United Kingdom
    Posts
    47
    ah iv done it! it was the $ i had on the end of the string which was ending the expression! removed it and it now works. here is the finished full code for anyone interested!

    Code:
    <script type="text/javascript">
    
    //<![CDATA[
    		   
    // create a regular expression to check UK postcode format
    var regExpressPostcode = /^([a-zA-Z]){2}([0-9][0-9]|[0-9]){1}\s([0-9][a-zA-z][a-zA-z]|[0-9][0-9][a-zA-z][a-zA-z]){1}$/;
    		   
    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 regExpressMilton = new RegExp("mk(1|2|3|4|5|6|7|8|9|10|11|12|13|15|17|19|77)[^(0-9)]");
    
    var regExpressMilton = /^MK(1|2|3|4|5|6|7|8|9|10|11|12|13|15|17|19|77)[^(0-9)]/;
     
    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
                    }
    
    // check for a valid postcode address format using the RegExp object method 'test()' 
            if (!regExpressPostcode.test(aForm.shipPostalCode.value)){
            window.alert("Please enter a correctly formatted UK postcode: eg. AB12 2BA or AB12 12AB")
                    //gives focus to field and selects incorrect postcode format text
            aForm.shipPostalCode.focus()
            aForm.shipPostalCode.select()
            return false
                    }
    				
    		if (regExpressMilton.test(aForm.shipPostalCode.value)) {
    		window.alert("Congratulations! You have been awarded a special gift with your order!")	
    		return true
    		}
            return true
            }
    		
    
    	//]]>	
    </script>
    Freelance Web Design/Developer - www.mikeoram.co.uk

  12. #12
    Join Date
    Apr 2009
    Posts
    96
    HTML Code:
    <html>
    <head>
    <script language="JavaScript" type="text/javascript">
    var regExpressMilton = new RegExp("(mk|MK)(1|2|3|4|5|6|7|8|9|10|11|12|13|15|17|19|77)[^(0-9)]");
     function checkForm() {
    var postalcode = document.getElementById("shipPostalCode").value;
    var a = regExpressMilton.test(postalcode);
    if (a) {
    	window.alert("congratulations! You have been awarded a special gift with your order!");	
    }
    return true
    }
    </script>
    </head>
    <body>
    <form id="form1">
    <input id="shipPostalCode" />
    <br />
    <a onclick="checkForm();">Check</a>
    </form>
    </body>
    </html>

  13. #13
    Join Date
    Apr 2009
    Posts
    96
    ah cool good good

  14. #14
    Join Date
    Nov 2010
    Location
    Bristol, United Kingdom
    Posts
    47
    yup got it thank you! all working now! cheers for the help!! i have implimented the (mk|MK) as you suggested also!

    Thank you once again!!
    Freelance Web Design/Developer - www.mikeoram.co.uk

  15. #15
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    If you do this, you can get rid of the "(mk|MK)" and append the mask with /i (case insensitive) which will test for "mk", "MK", "mK", and "Mk".
    Code:
    var regExpressMilton = /mk(1|2|3|4|5|6|7|8|9|10|11|12|13|15|17|19|77)[^(0-9)])/i;
    http://www.w3schools.com/jsref/jsref_regexp_test.asp
    |||||
    o . Q
    ___ "You live and you learn; or you don't live long." - Lazarus Long

    ^_^

Thread Information

Users Browsing this Thread

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

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
  •  
HTML5 Development Center



Recent Articles