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

How to flood IP addresses to find server

python networking ip client server

I am trying to use brute force to generate a range of local ip address in my client with the hope of finding the server's IP which is waiting for a connection.I already know the port number at whic...

Fastest possible dictionary-like matching

python algorithm optimization data-structures spell-checking

I will have to perform a spelling check-like operation in Python as follows:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.