www.webdeveloper.com
Results 1 to 5 of 5

Thread: Matching first part of text input field

  1. #1
    Join Date
    Nov 2013
    Posts
    3

    Matching first part of text input field

    I've got a piece of javascript that throws up an alert if a text box input matches a certain value (in this case it's a postcode) however I need it to only check the first couple of characters of the input field rather than matching the whole lot.
    Here's the code
    Code:
    $(window).load(function(){
    jQuery('#zipc').focusout(function () {
        var _val = jQuery(this).val();
        var _array = ["BF1 3AA", "SA11 3PD"];
        for (var i = 0; i < _array.length; i++) {
            if (_val.indexOf(_array[i]) != -1) {
                alert('Your postcode shows you live in a remote location.\nDepending on how bulky or heavy your purchase is, this may require us to contact you for a shipping surcharge.\r\nThis will be no more than 65 and is often much less');
                jQuery('#zipc').val("");
            }
        }
    });
    });
    The codes I need to check for are

    AB,DD,FK,IV,KW,KY,PA,PH,SA,TR

    so if someone entered AB11 1ND then the alert message would be triggered but NOT if they entered GL11 1AB (with the letters matching later in the string)

    Can someone suggest the mod I need to make to the script for this?!

    Cheers

    Jos

  2. #2
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    I didn't use all the jQuery code, but I'm sure you can see where you can put this:

    Code:
    <script type="text/javascript">
    
    	var _val = 'AB11 1ND'; //SHOWS THE ALERT
    //	var _val = 'GL11 1AB'; //DOES NOT SHOW ALERT
    
    	if (/^(AB|DD|FK|IV|KW|KY|PA|PH|SA|TR)/i.test(_val)) {
    		alert('Your postcode shows ... often much less');
    	}
    
    </script>
    Last edited by bionoid; 11-11-2013 at 01:56 PM.
    JavaScript: Learn | Validate | Compact | bionoid

  3. #3
    Join Date
    Nov 2013
    Posts
    3
    Bionoid - that's great, tyvm...

    Just to clarify, the vars that you showed for _val - I obviously don't know what postcode that someone might enter - just that if it begins with one of the prefixes I mentioned that it has to throw up the alert.

    Is there not an array I can use, something like

    Code:
    var _array = ["AB*", "DD*","FK*","IV*","KW*","KY*","PA*","SA*","TR*"];
    which would just match anything after those first 2 characters?

    (As you can tell - I'm no coder so just trying to figure out an easy way to expand the script if necessary!

    Thanks

    Jos

  4. #4
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    Seeing as you're using a static array of possible values, I don't see why you can't just expand the regular expression to perform the same checks.

    For example, the current expression is just checking if the postcode starts^ with any of the predefined set of characters:

    Code:
    /^(AB|DD|FK|IV|KW|KY|PA|PH|SA|TR)/i.test(_val)
    But if you want to make sure they also entered a couple numbers after them, you can change it to:

    Code:
    /^(AB|DD|FK|IV|KW|KY|PA|PH|SA|TR)\d+/i.test(_val)
    There are obviously many other possibilities with a regular expression, just need to know what exactly needs checking for.

    Link: Regular Expression Syntax
    Last edited by bionoid; 11-11-2013 at 05:21 PM.
    JavaScript: Learn | Validate | Compact | bionoid

  5. #5
    Join Date
    Nov 2013
    Posts
    3
    OK - so the following code seems to work fine
    Code:
    $(window).load(function(){
    jQuery('#postcode').focusout(function () {
    	  var _val = jQuery(this).val();
    		if (/^(AB|DD|FK|IV|KW|KY|PA|PH|SA|TR)\d+/i.test(_val)) {
    			alert('Your postcode shows you live in a location where our suppliers/carriers may add a delivery surcharge.\n\nIf this applies to your order, a member of our team will contact you to discuss the surcharge before your order is processed.\n\nPlease refer to our Terms and Conditions or the Delivery and Returns Section for more details');
    		}
      });
      });//]]>
    Many thanks for your help!

    Jos

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