Adding custom validation to form fields



Show first post

112 replies

Userlevel 7
Badge +3

Hi Joe, 

It turns out you need the () parentheses but also to wrap the RegEx in slashes / /.

I just tested it on this page and it seems to work: http://unbouncepages.com/custom-validation-test/

Here is the exact script:

&nbsp;&nbsp;<br />&nbsp; lp.jQuery(function($) {<br />&nbsp;&nbsp;<br />&nbsp; &nbsp; var ruleID = 'custom';<br />&nbsp; &nbsp; var field = 'validation_rule_test';<br />&nbsp; &nbsp; var message = 'Please enter a valid number';<br />&nbsp;&nbsp;<br />&nbsp; &nbsp; var rules = module.lp.form.data.validationRules[field];<br />&nbsp;&nbsp;<br />&nbsp; &nbsp; $.validator.addMethod(ruleID, function(value, field) {<br />&nbsp;&nbsp;<br />&nbsp; &nbsp; &nbsp; var valid = ( /[a-zA-Z]{6}[0-9]{2}[a-zA-Z][0-9]{2}[[a-zA-Z][0-9]{3}[a-zA-Z]/.test(value) );<br />&nbsp;&nbsp;<br />&nbsp; &nbsp; &nbsp; return valid || (!rules.required && !value);<br />&nbsp; &nbsp; }, message);<br />&nbsp;&nbsp;<br />&nbsp; &nbsp; rules[ruleID] = true;<br />&nbsp;&nbsp;<br />&nbsp; });<br />

If you make the above changes, it should work. 

Best,
Hristian

P.S. Make sure you adjust the ruleID, field and message.

Hi Hristian,

I tried closing it with parentheses on both ends as you suggest, but still does not work

any other suggestions?

thanks for your time

J

Userlevel 7
Badge +3

Hi Joe, 

I just checked your RegEx with this tool and it seems to be working as intended. 

![](https://d2r1vs3d9006ap.cloudfront.net/s3_images/1328514/RackMultipart20151214-14187-obpi08-Online_regex_tester_and_debugger JavaScript Python PHP and_PCRE_inline.png?1450078755 “Image: https://d2r1vs3d9006ap.cloudfront.net/s3_images/1328514/RackMultipart20151214-14187-obpi08-Online_regex_tester_and_debugger__JavaScript__Python__PHP__and_PCRE_inline.png?1450078755”)

Have you tried closing it with parentheses on both ends (). 

![](https://d2r1vs3d9006ap.cloudfront.net/s3_images/1328515/RackMultipart20151214-26309-jc3rnz-This_validates_a_field_for_currency_for_dollar_and_cents Ex 42_50___GitHub_inline.png?1450078768 “Image: https://d2r1vs3d9006ap.cloudfront.net/s3_images/1328515/RackMultipart20151214-26309-jc3rnz-This_validates_a_field_for_currency_for_dollar_and_cents__Ex__42_50___GitHub_inline.png?1450078768”)

That’s the only thing I can spot out of place with the original script example. 

Best,
Hristian

cancelled

Hi Johnny,

I tried to use your script to validate the field ‘cod_fisc’ in my form, which must be an alphanumeric string as follows:
[6 letters] [2 numbers] [1 letter] [2 numbers] [1 letter] [3 numbers] [1 letter]

below my script:

however, it does not function. Where am I wrong?

thanks for your help

Johnny, you’re the man!

Thanks, works!!

Userlevel 3

Thanks for clarifying Hayley

As this is something I can see asked for more frequently, I whipped up a validation myself

https://gist.github.com/johnnyopao/c9965db11cded79aede6

Be sure to update the IDs with your own email fields

We want the two field to match exactly. We have an “email” field and then a “confirm email” field. So the custom validation needs to “match the exact value as another field”. Does that make sense?

email field 1 == email field 2

Userlevel 3

Hey Hayley - Sorry i’m a bit confused as to what you’re trying to do.

Did you want to check if another field to see if it was filled out? Or you want this field field to match the exact value as another field? Or something else completely?

In the example above, how would I change “I Accept” to check another field for matching information (i.e. email confirm field). I already added another field var, but can’t seem to get the snytax right to replace 'I Accept". Thanks.

Userlevel 3

Here’s a couple others

Digits only
https://gist.github.com/johnnyopao/51…

Currency up to two decimals (ex: 45.50)
https://gist.github.com/johnnyopao/86…

Confirm email
https://gist.github.com/johnnyopao/c9…

Reply