| 2 |
lars |
1 |
/**
|
|
|
2 |
* Validates currencies with any given symbols by @jameslouiz
|
|
|
3 |
* Symbols can be optional or required. Symbols required by default
|
|
|
4 |
*
|
|
|
5 |
* Usage examples:
|
|
|
6 |
* currency: ["£", false] - Use false for soft currency validation
|
|
|
7 |
* currency: ["$", false]
|
|
|
8 |
* currency: ["RM", false] - also works with text based symbols such as "RM" - Malaysia Ringgit etc
|
|
|
9 |
*
|
|
|
10 |
* <input class="currencyInput" name="currencyInput">
|
|
|
11 |
*
|
|
|
12 |
* Soft symbol checking
|
|
|
13 |
* currencyInput: {
|
|
|
14 |
* currency: ["$", false]
|
|
|
15 |
* }
|
|
|
16 |
*
|
|
|
17 |
* Strict symbol checking (default)
|
|
|
18 |
* currencyInput: {
|
|
|
19 |
* currency: "$"
|
|
|
20 |
* //OR
|
|
|
21 |
* currency: ["$", true]
|
|
|
22 |
* }
|
|
|
23 |
*
|
|
|
24 |
* Multiple Symbols
|
|
|
25 |
* currencyInput: {
|
|
|
26 |
* currency: "$,£,¢"
|
|
|
27 |
* }
|
|
|
28 |
*/
|
|
|
29 |
$.validator.addMethod( "currency", function( value, element, param ) {
|
|
|
30 |
var isParamString = typeof param === "string",
|
|
|
31 |
symbol = isParamString ? param : param[ 0 ],
|
|
|
32 |
soft = isParamString ? true : param[ 1 ],
|
|
|
33 |
regex;
|
|
|
34 |
|
|
|
35 |
symbol = symbol.replace( /,/g, "" );
|
|
|
36 |
symbol = soft ? symbol + "]" : symbol + "]?";
|
|
|
37 |
regex = "^[" + symbol + "([1-9]{1}[0-9]{0,2}(\\,[0-9]{3})*(\\.[0-9]{0,2})?|[1-9]{1}[0-9]{0,}(\\.[0-9]{0,2})?|0(\\.[0-9]{0,2})?|(\\.[0-9]{1,2})?)$";
|
|
|
38 |
regex = new RegExp( regex );
|
|
|
39 |
return this.optional( element ) || regex.test( value );
|
|
|
40 |
|
|
|
41 |
}, "Please specify a valid currency" );
|