Exclude form field values beginning with '06'


#1

I was wondering if someone could tell me what the javascript would be to validate a field to exclude certain numbers. I want to exclude CT zip codes. So any zip code entered into the form field beginning with “06” would be validated. The user would see an error message and wouldn’t be able to submit the form.

Anyone know how to modify this script to achieve the above?
https://gist.github.com/markwainwrigh…

Many thanks to whoever can answer.


#2

anyone?


#3

Hi Rod,

I hacked together a validation script that should work for this. Please be sure to test it out thoroughly before using it on your page, though!

// Update 'zip' to match your email field's ID <br />
var field = 'zip'; <br />
var message = 'Please enter a non-CT ZIP code'; <br /><br />
// Define the rule and apply it to the field <br />
$.validator.addMethod('notCTZip', function(value) { <br />
  return !/^06/.test(value); <br />
}, message); <br /><br />
var rule = module.lp.form.data.validationRules[field]; <br />
if ( rule ) rule.notCTZip = true; <br /><br /> }); <br /> </script>```   
 
That'll just check if the field starts with '06' or not - if you want to check whether it's a valid ZIP code otherwise, you'll want to use it alongside this script: [https://gist.github.com/markwainwrigh...](https://gist.github.com/markwainwright/10073472)

#4

Thanks Mark, works perfect!


#5

Hi Mark,

Wondering if I can request an update to this script:

I would like to display the same error message if the zip code value begins with ‘06’, but *only* if another form field starts with ‘06’ as well.

Possible?

Thanks!
Rod


#6

Hi Rod,

This is getting a bit complicated, so I don’t think we’ll be able to offer too much support beyond here.

But I had the chance to think about it, and you could try this approach:

return !/^06/.test(value) || !/^06/.test( $('#other_field').val() );

(replace line 8 of the code above with that version, and ‘other_field’ with the ID of that other field in your form).

However, I haven’t been able to test it myself, so I’d recommend testing it thoroughly before using it (i.e. with each combination of the two fields). Hope it gets you pointed in the right direction!


#7

Many thanks Mark. If I may, just one more quick request!

As a modificaiton to the first script you posted, if I want to exclude all zip codes from that field *except* for zips beginning with 07 and 08 for eg., what is the modifcation I should make?

Thanks so much!
Rod


#8

Hi Rod,

The validation rules you’re looking for are getting fairly complex so it’s out of the scope of the support we can offer - sorry.

If you have access to some JavaScript development expertise then you could start by looking at this page: http://jqueryvalidation.org/jQuery.va… It’s the API reference for how to add custom methods to the jQuery validation plugin that we use. That’s what the code in my initial reply was based on.