Domanda

I'm trying to set some JQuery validation to check UK postcodes that are entered into a form. So from what I know about Postcodes they are at least 5 chars long but no longer than 7 and all contain at least 3 Letters, so what I have so far is:

// Postcode Validation
$('#regpostcode').blur(function () {
    $(this).val($(this).val().replace(/\s/g, ""));
    var regpostcode = $(this).val();
    if (regpostcode.length < 5) {
        $('.regPostcodeStatus').html("Please Enter Your Actual Postcode").removeClass("success").addClass("error");
        regpostcodeok = 2;
    } else if (regpostcode.length > 7) {
        $('.regPostcodeStatus').html("Please Enter Your Actual Postcode").removeClass("success").addClass("error");
        regpostcodeok = 2;
    } else {
        $('.regPostcodeStatus').html("Thank You").removeClass("error").addClass("success");
        regpostcodeok = 1;
    }
});
$('#regpostcode').keyup(function (e) {
    $("#regpostcode").val(($("#regpostcode").val()).toUpperCase());
});

As you can see, this sorts out the problem of having a too long or too short entry but doesn't sort out the problem of having AT LEAST 3 letters. Any ideas how this can be accomplished?

È stato utile?

Soluzione

Try adding this validation :

if(regpostcode.match(/[a-z]/gi).length < 3) //Error

Match will create an array of every time the regexp is found in the string. So checking the array length will give you the number of letter.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top