New Zealand Phone Form Validation

I am building a form system and I have now started to work on the validation. My framework is CI so for the main part of my validation I have 'trim|required|htmlspecialchars' but what I was wanting to know:

A) What would be best to use for url and phone* inputs

B) What would you recommend that I use for client side validation - if any at all?

*I am from New Zealand so our phone numbers are 00 123 4567 or 1234567

Answers


CI's validation is seriously shitty to deal with, especially when you want to make use of awesome jquery pluggins that make life simple.

For client side, I make use of a jquery plugin written by Jörn Zaefferer, it is pretty easy to get the hang of and is very customizable.

There are plenty of built in methods for standard things like urls, for NZ phone numbers I created some custom rules you can add to the additional-methods.js file that comes with the plugin.

/**
 * matches NZ phone number formats
 * 
 * +64 7 123 1234
 * or
 * 07-123-1234
 * or
 * 071231234
 * 
 * spaces/hyphens optional
 *
 */
jQuery.validator.addMethod('phoneNZ', function(phone_number, element) {
return this.optional(element) || phone_number.length > 9 &&
phone_number.match(/^(0|(\+64(\s|-)?)){1}\d{1}(\s|-)?\d{3}(\s|-)?\d{4}$/, 'Please specify a valid phone number');


/**
 * matches NZ mobile number formats
 *  
 * 021 123 1234
 * or
 * 027 123 1234
 * or
 * 022-123-1234
 * or
 * +64 21 123 1234
 *
 * spaces/hyphens optional
 * 
 */
jQuery.validator.addMethod('mobileNZ', function(phone_number, element) {
return this.optional(element) || phone_number.length > 9 &&
phone_number.match(/^(0|(\+64(\s|-)?)){1}(21|22|27){1}(\s|-)?\d{3}(\s|-)?\d{4}$/);
}, 'Please specify a valid mobile number');

As a side note, use these sparingly, unless you plan on dialing these numbers computationally just leave them as free text with a limit on length, there are a lot of variations on how people want to enter phone numbers and you're better off not pissing people off by enforcing a rule that does not suit them / their number / addition of letters / + symbols / extentions etc


Need Your Help

Renaming a file after IF EXIST check within ++ FOR loop in a Windows batch file?

if-statement batch-file for-loop batch-rename

I scoured the web and could not find the exact code I'm looking for... I found things that are very similar but did not get them to work. Here is a rough idea what I want:

Adding Community Members to a community activity using the REST API gives 403

api member ibm-connections

We want to add community members (as author) to a community activity.