Validation rules questions

sinemac 24 Mar, 2009
1. Is it possible to set validation for a single field that has two different formats?

We have a field for 'Postal/ZIP Code' and want to be able to accept either Canadian Postal Codes or U.S. ZIP Codes, and would like to make sure the submission conforms to one or the other.

Is this possible with one field? Or would we need to have two separate fields?

2. Can you give me some guidance on how to do a custom validation rule? I want to validate a field for phone numbers, to ensure they're in xxx-xxx-xxxx format.

Thanks,
Scott
GreyHead 24 Mar, 2009
Hi Scott,

Possibly - Google Dexagogo for the help for Andew Tetlaw's Really Easy Validation

Bob
sinemac 24 Mar, 2009
Thanks Bob. I'll check it out.

Scott
sinemac 24 Mar, 2009
The Google groups for Dexagogo had exactly what I needed for both. I added the code to mooValidation.js and mooValidation2.js -- and it works fine. Just wondering if it's actually needed in both files?

Now, of course, I'll need to set some kind of reminder so that I don't lose it with the next Chronoforms update! 😀

Thanks again Bob - a great help as always!!


Scott
GreyHead 24 Mar, 2009
Hi Scott,

You can add an 'extra' validation with code in your form - I don't have time to find it right now but I think I posted an example here for password matching. That way it's safe if you upgrade.

Bob
sinemac 24 Mar, 2009
Thanks again, Bob.

I found the post you referenced (I think - or at least one you posted with the necessary content :wink: )

In case anyone else is searching for this info, and the code needed for Postal/ZIP codes and Cdn/US phone number validation, here's what I added to the Form JavaScript field:
Validation.add('validate-phone-us', 'Please enter a valid phone number (xxx)xxx-xxxx', function(v) { 
				var regex = /^(\()?(\d{3})([\)-\. ])?(\d{3})([-\. ])?(\d{4})$/; 
				return Validation.get('IsEmpty').test(v) || regex.test(v); 
            });

Validation.add('validate-us-ca-postal', 'Please enter a valid Canadian postal code or US zip code.', function(v,elm) {
				v = v.toUpperCase();
				var regex = /((^\d{5}([- |]\d{4})?$)|(^[A-Z]\d[A-Z][- |]\d[A-Z]\d$))/;
				if(Validation.get('IsEmpty').test(v) ||  regex.test(v)){
				$(elm).value = v; 
                	return true; 
                } 
                return false;
			});


Then just added the necessary class attribute to the fields in the Form HTML, e.g.,
<input type="text" class="validate-phone-us" id="phone" name="phone" />


Works wonderfully!

Thanks,
Scott
Rosebud 29 Mar, 2009
Thanks, Scott, for working this out and posting. You saved me a bunch of time and I am most appreciative. Chronoforms is a great extension and I'm very impressed by the forum responses and support.

Bob and Max you do a great job! Thank you.

Cheers!
Rosie
bobelly 08 Apr, 2009
Hey,

I used the script shown here. It works, sort of. I tested it out, and it doesn't actually check for invalid characters in the postal code - F,D,Q, etc. (Canadian postal codes do not use the following letters: D, F, I, O, Q, and U)

In fact, the only postal code validation it does for me is a check that it is in the format XXX XXX and that it uses an alternating letter and number.
I replaced the regular expression with this:

var regex = /^\d{5}([\-]\d{4})?$|[ABCEGHJ-NPRSTVXY]{1}[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[ ]?[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[0-9]{1}$/;

It now does correct postal code validation.
Max_admin 09 Apr, 2009
Thanks for posting the fix!🙂

Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
This topic is locked and no more replies can be posted.

VPS & Email Hosting 20% discount
hostinger