0

In e-commerce system, we have list of postcode and we need to exclude 2 range of numbers for postcode.

For example: Postcode excluded starting with 1xxxxx & 3xxxxx, but accepted from 6xxxxx.

I have validation code in javascript and needed to add 2 conditions, if number start with 1 and 3, the user have warning message that this postcode not in coverage.

Hope anyone could help.

Code I have currently as below:

 return !Validation.get('IsEmpty').test(v) &&  /^60/.test(v);

Thanks.

1
  • if (v.match(/[13]\d+/) { alert('Not in Coverage') Commented Oct 19, 2016 at 4:47

2 Answers 2

1

I would like to share my Experience of verifying US postal Codes in Jquery that I have used during a project in Oodles Technologies.

I have solved this problem by using jQuery validation plugin

$("#myform").validate({
rules: {
field: {
required: true,
phoneUS: true
}
}
});

Files required for this

<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://jqueryvalidation.org/files/dist/jquery.validate.min.js"></script

It sometimes doesn't work.

To get it working we have to include Jquery addtional-methods.min.js If still it doesn't work then you have to use following script before validate method which contains regex to validate.

jQuery.validator.addMethod("phoneUS", function (phone_number, element) {
            phone_number = phone_number.replace(/\s+/g, "");
            return this.optional(element) || phone_number.length > 9 &&
                  phone_number.match(/\(?[\d\s]{3}\)[\d\s]{3}-[\d\s]{4}$/);
        }, "Invalid phone number");

It finally solve the problem. If you have required some other format then modify above regex according to need.

Sign up to request clarification or add additional context in comments.

Comments

0

This will check whether v is as any number([0-9]) except 1 or 3 as the first digit

return !Validation.get('IsEmpty').test(v) && /^[0-9]/.test(v) && !(/^[13]/.test(v))

This will check whether v is as partial of digit as first(6,7,8,9)

return !Validation.get('IsEmpty').test(v) && /^[6789]/.test(v)

By the way, you might also want to check the length and format of the post code in 6 digits({5}). In that case, you can change the phrase as

return !Validation.get('IsEmpty').test(v) && /^[6789][0-9]{5}$/.test(v)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.