Field validation for length of entry and also check for a specific three character prefix in the beginning


Hi,  I need a field validation that checks if the entry is 17 characters and also checks that the first three characters match the prefix “ABC”.

Anyone have any existing scripts that i can modify to get this working?

I really appreciate it and I can guarantee 1000 karma points to you for the help 🙂


7 replies

Whoa, 1000 karma points? Whatta deal! 

I’ll ping a few folks from our Community that know their way around some code. Hopefully we can get you on the right path!

Thanks Justin, you are the best!

Userlevel 3

1000 Karma points!?! I’ll take it!

 

 lp.jQuery(function($) {<br />&nbsp; &nbsp; var ruleID = 'unique_code';<br />&nbsp; &nbsp; var field = 'code_field';<br />&nbsp; &nbsp; var message = 'The code appears to be invalid';<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; &nbsp; var valid = ( /^ABC.{14}$/i.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; });<br />

There’s my go at it. Replace ‘code_field’ with the field id/name you want to assign the validation to.

Can’t promise it’s perfect so be sure to test it out. I made it case insensitive so the ABC can be uppercase or lowercase mix. If you want to be strict about the casing simply strip out the ‘i’ at the end of the regular expression

/^ABC.{14}$/i

* awaits karma *

Whoa, Johnny! Soooo awesome! I was saving these for a rainy day but… you can take all of my karma points.

 

Johnny you are amazing, thank you so much.  Sending you #allthekarma!

Please can you help me modify this code so that it only validates for a 10 digit telephone number?

It would help us enormously!

Thanks,

Hey there Daniel! The North American phone number validator that’s built into our form builder will validate phone numbers that are 10 digits in length. This validator will assume that all phone numbers that match the number of required digits in a North American phone number is a valid entry, and it will not validate any phone numbers that exceed 10 digits or have fewer than 10 digits. I hope this helps! If you’re looking for something more specific, I’ll loop in Johnny, and see if he can chime in here. 

Reply