Bringing romance back .... to forms.
View the Project on GitHub omarshammas/jquery.formance
Read here how to get started
A general purpose library for formatting and validating form fields, based on / inspired by Stripe's jQuery.payment library.
Client side validation is not sufficient in any project because the javascript can be bypassed and people can submit requests directly to the server. However, that doesn't mean client side validation should be forgotten. This library is for those who care about the user experience.
Formats the phone number in the (111) 111 - 1111
format :
(
and )
around the area code-
between the prefix (first 3 digits after the area code) and the line number (last 4 digits)
$('input.phone_number').formance('format_phone_number');
Validates the phone number:
$("<input value='6131231234' />").formance('validate_phone_number'); // true
$("<input value='(613) 123 - 1234' />").formance('validate_phone_number'); // true
$("<input value='(613) 123 - 12345' />").formance('validate_phone_number'); // false
Formats the date in the dd / mm / yyyy
format:
/
between the day & month and month & year.
$('input.dd_mm_yyyy').formance('format_dd_mm_yyyy');
Validates the date:
$('input.dd_mm_yyyy').formance('validate_dd_mm_yyyy');
Date includes a helper function to retrieve the date value. Note it does not check to see if it is valid. It simply creates a date object with the specified values which may be incorrect. For example new Date(2013, 2-1, 30)
returns March 2nd because February does not have 30 days. You should check if it is valid before using the date.
If the text is parsed without any errors then a Javascript Date object is returned otherwise false.
$("<input value='01 / 07 / 2013' />").formance('val_dd_mm_yyyy'); // new Date(2013, 7-1, 1)
$("<input value='dd / 07 / 2013' />").formance('val_dd_mm_yyyy'); // false
You cannot format an email address field because there is no structure.
Before everybody goes on a rant, I agree that the only way to validate an email address is to send it an email. However, some client side validation does not hurt especially since 99% of email addresses won't have multiple @
symbols and be top level domains.
If you want to contribute another email validator by all means do so. It would be helpful to have multiple email validators with varying degrees of coverage.
Validates the email address:
formance_algorithm
. By default the simple
algorithm is used.simple
and complex
.
$("<input value='john@example.com' />").formance('validate_email'); // true
$("<input value='john@example.com' data-formance_algorithm='simple' />").formance('validate_email'); // true
$("<input value='john@example.com' data-formance_algorithm='complex' />").formance('validate_email'); // true
$("<input value='postbox@com' data-formance_algorithm='simple' />").formance('validate_email'); // true
$("<input value='postbox@com' data-formance_algorithm='complex' />").formance('validate_email'); // false
Formats the credit card cvc:
$('input.credit_card_cvc').formance('format_credit_card_cvc');
Validates the credit card cvc:
credit_card_type
exists to check if cvc matches that card's standards.
$("<input value='123' />").formance('validate_credit_card_cvc'); // true
$("<input value='1234' />").formance('validate_credit_card_cvc'); // true
$("<input value='123' data-credit_card_type='amex' />").formance('validate_credit_card_cvc'); // true
$("<input value='1234' data-credit_card_type='amex' />").formance('validate_credit_card_cvc'); // true
$("<input value='12345' />").formance('validate_credit_card_cvc'); // false
Formats the credit card expiry date in the mm / yyyy
format:
/
between the month & year$('input.credit_card_expiry').formance('format_credit_card_expiry');
Validates the credit card expiry date:
mm / yy
$('input.credit_card_expiry').formance('validate_credit_card_expiry');
//examples
$("<input value='05 / 20' />").formance('validate_credit_card_expiry'); // true
$("<input value='05 / 2020' />").formance('validate_credit_card_expiry'); // true
$("<input value='05 / 1900' />").formance('validate_credit_card_expiry'); // false
$("<input value='mm / 20' />").formance('validate_credit_card_expiry'); // false
Formats the credit card card number:
$('input.credit_card_number').formance('format_credit_card_number');
Validates the credit card number:
$('input.credit_card_expiry').formance('validate_credit_card_number');
//examples
$("<input value='4242424242424242' />").formance('validate_credit_card_number'); // true
$("<input value='4242 4242 4242 4242' />").formance('validate_credit_card_number'); // true
$("<input value='4242-4242-4242-4242' />").formance('validate_credit_card_number'); // true
$("<input value='4242' />").formance('validate_credit_card_number'); // false
This field includes a special helper function to retrieve the credit card type. It recognizes
visa
mastercard
discover
amex
dinersclub
maestro
laser
unionpay
The function will return null
if the card type can't be determined.
$.formance.creditCardType('4242 4242 4242 4242') // 'visa'
Formats the number:
$('input.number').formance('format_number');
Validates the number:
formance_length
data attribute exists, then it will check that the input length matches what was specified. It must be a number.
$("<input value='1234' />").formance('validate_number'); // true
$("<input value='1234' data-formance_length=4 />").formance('validate_number'); // true
$("<input value='1234' data-formance_length=5 />").formance('validate_number'); // false
$("<input value='1234a' />").formance('validate_number'); // false
Formats the postal code in the A1A 1A1
format:
According to http://stackoverflow.com/questions/1146202/canada-postal-code-validation.
$('input.postal_code').formance('format_postal_code');
Validates a postal code field:
$("<input value='a1a1a1' />").formance('validate_postal_code'); // true
$("<input value='A1A 1A1' />").formance('validate_postal_code'); // true
$("<input value='a1a1' />").formance('validate_postal_code'); // false
Formats the Ontario Driver's License Number in the format A1234 - 12345 - 12345
:
-
every 5 characters.
$('input.odln').formance('format_ontario_drivers_license_number');
Validates the Ontario Driver's License Number:
$("<input value='A12341234512345' />").formance('validate_ontario_drivers_license_number'); // true
$("<input value='A1234 - 12345 - 12345' />").formance('validate_ontario_drivers_license_number'); // true
$("<input value='A1234 - 12345 - 123456' />").formance('validate_ontario_drivers_license_number'); // false
$("<input value='A1234 - 1234 - 12345' />").formance('validate_ontario_drivers_license_number'); // false
Formats the Ontario Photo Health Card Number in the format 1234 - 123 - 123 - AB
:
-
after the first 4 characters, and every 3 characters going forward.
$('input.ophcn').formance('format_ontario_photo_health_card_number');
Validates the Ontario Photo Health Card Number:
$("<input value='1234123123AB' />").formance('validate_ontario_photo_health_card_number'); // true
$("<input value='1234 - 123 - 123 - AB' />").formance('validate_ontario_photo_health_card_number'); // true
$("<input value='1234 - 123 - 123 - ABC' />").formance('validate_ontario_photo_health_card_number'); // false
$("<input value='12 - 123 - 123 - AB' />").formance('validate_ontario_photo_health_card_number'); // false
Formats the Ontario Outdoors Card Number in the format 708158 123456789
:
708158
after the standard first 6 characters.
$('input.oocn').formance('format_ontario_outdoors_card_number');
Validates the Ontario Outdoors Card Number:
708158
$("<input value='708158123456789' />").formance('validate_ontario_outdoors_card_number'); // true
$("<input value='708158 123456789' />").formance('validate_ontario_outdoors_card_number'); // true
$("<input value='708158 123456789012' />").formance('validate_ontario_outdoors_card_number'); // false
$("<input value='708158 123456' />").formance('validate_ontario_outdoors_card_number'); // false
$("<input value='708158 123456789abce' />").formance('validate_ontario_outdoors_card_number'); // false
Formats a time in years and months:
Useful for inputting time in years and months. For example, duration of employment or residency.
$('input.time_yy_mm').formance('format_time_yy_mm');
Validates the time in years and months:
$('input.time_yy_mm').formance('validate_time_yy_mm');
//examples
$("<input value='09 / 09' />").formance('validate_time_yy_mm'); // true
$("<input value='10 /' />").formance('validate_time_yy_mm'); // false
Formats the UK sort code:
$('input.uk_sort_code').formance('format_uk_sort_code');
Validates the UK sort code:
$('input.uk_sort_code').formance('validate_uk_sort_code');
//examples
$("<input value='09 - 09 - 09' />").formance('validate_uk_sort_code'); // true
$("<input value='00 -' />").formance('validate_uk_sort_code'); // false