10+ Useful JavaScript Regular Expression Functions to improve your web applications efficiency
Fortunately, JavaScript 1.2+ has incorporated regular
expressions.JavaScript has an alternate syntax for creating Regular
Expression objects that implicitly calls the RegExp constructor
function.A regular expression pattern is composed of simple characters,
such as
To use the Regular Expression object to validate the user input you
must be able to define a pattern string that represents the search
criteria. Patterns are defined using string literal characters and
metacharacters. The following is some useful regular expression based
javascript function, some works like validating user input will be very
simple by using them.
No.1: Check if string is non-blank
No.2: Check if string is a whole number(digits only).
or as seen in the following snippet:
No.3: Checks that an input string is an integer
or as seen in the following snippet:
No.4: Checks that an input string is a decimal number
No.5: Check if string is currency
works just like
No.6: Checks that an input string looks like a valid email address
or as seen in the following snippet:
No.7: Check for valid credit card type/number
or as seen in the following snippet:
This is the luhn checksum algorithm, used to validate such things as credit card numbers and bank routing numbers.
No.8: Returns a string with everything but the digits removed
No.9: Get String Length // Get String Length
http://ntt.cc/2008/05/10/over-10-useful-javascript-regular-expression-functions-to-improve-your-web-applications-efficiency.html
/abc/,
the syntax for that method is the following: 1 | var RegularExpression = /pattern/[ switch ] |
No.1: Check if string is non-blank
1 | // Check if string is non-blank |
2 | var isNonblank_re = /\S/; |
3 | function isNonblank (s) { |
4 | return String (s).search (isNonblank_re) != -1 |
5 | } |
1 | // Check if string is a whole number(digits only). |
2 | var isWhole_re = /^\s*\d+\s*$/; |
3 | function isWhole (s) { |
4 | return String(s).search (isWhole_re) != -1 |
5 | } |
1 | // check 0-9 digit |
2 | function regIsDigit(fData) |
3 | { |
4 | var reg = new RegExp(”^[0-9]$”); |
5 | return (reg.test(fData)); |
6 | } |
1 | // checks that an input string is an integer, with an optional +/- sign character. |
2 | var isInteger_re = /^\s*(\+|-)?\d+\s*$/; |
3 | function isInteger (s) { |
4 | return String(s).search (isInteger_re) != -1 |
5 | } |
1 | // check is number |
2 | function regIsNumber(fData) |
3 | { |
4 | var reg = new RegExp(”^[-]?[0-9]+[\.]?[0-9]+$”); |
5 | return reg.test(fData) |
6 | } |
1 | // Checks that an input string is a decimal number, with an optional +/- sign character. |
2 | var isDecimal_re = /^\s*(\+|-)?((\d+(\.\d+)?)|(\.\d+))\s*$/; |
3 | function isDecimal (s) { |
4 | return String(s).search (isDecimal_re) != -1 |
5 | } |
works just like
isDecimal
, except that only zero or two digits are allowed after the decimal point.1 | // Check if string is currency |
2 | var isCurrency_re = /^\s*(\+|-)?((\d+(\.\d\d)?)|(\.\d\d))\s*$/; |
3 | function isCurrency (s) { |
4 | return String(s).search (isCurrency_re) != -1 |
5 | } |
1 | // checks that an input string looks like a valid email address. |
2 | var isEmail_re = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/; |
3 | function isEmail (s) { |
4 | return String(s).search (isEmail_re) != -1; |
5 | } |
1 | // Check if string is a valid email address |
2 | function regIsEmail(fData) |
3 | { |
4 | var reg = new RegExp(”^[0-9a-zA-Z]+@[0-9a-zA-Z]+[\.]{1}[0-9a-zA-Z]+[\.]?[0-9a-zA-Z]+$”); |
5 | return reg.test(fData); |
6 | } |
1 | // Check for valid credit card type/number |
2 | var creditCardList = [ |
3 | //type prefix length |
4 | [ "amex" , "34" , 15], |
5 | [ "amex" , "37" , 15], |
6 | [ "disc" , "6011" , 16], |
7 | [ "mc" , "51" , 16], |
8 | [ "mc" , "52" , 16], |
9 | [ "mc" , "53" , 16], |
10 | [ "mc" , "54" , 16], |
11 | [ "mc" , "55" , 16], |
12 | [ "visa" , "4" , 13], |
13 | [ "visa" , "4" , 16] |
14 | ]; |
15 | function isValidCC (cctype, ccnumber) { |
16 | var cc = getdigits (ccnumber); |
17 | if (luhn (cc)) { |
18 | for ( var i in creditCardList) { |
19 | if (creditCardList [i][0] == (cctype.toLowerCase ())) { |
20 | if (cc.indexOf (creditCardList [i][1]) == 0) { |
21 | if (creditCardList [i][2] == cc.length) { |
22 | return true ; |
23 | } |
24 | } |
25 | } |
26 | } |
27 | } |
28 | return false ; |
29 | } |
This is the luhn checksum algorithm, used to validate such things as credit card numbers and bank routing numbers.
1 | function luhn (cc) { |
2 | var sum = 0; |
3 | var i; |
4 |
5 | for (i = cc.length - 2; i >= 0; i -= 2) { |
6 | sum += Array (0, 2, 4, 6, 8, 1, 3, 5, 7, 9) [parseInt (cc.charAt (i), 10)]; |
7 | } |
8 | for (i = cc.length - 1; i >= 0; i -= 2) { |
9 | sum += parseInt (cc.charAt (i), 10); |
10 | } |
11 | return (sum % 10) == 0; |
12 | } |
1 | // This returns a string with everything but the digits removed. |
2 | function getdigits (s) { |
3 | return s.replace (/[^\d]/g, “”); |
4 | } |
1 | // Get String Length |
2 | function regGetStrLength(fData) |
3 | { |
4 | var valLength = fData.length; |
5 | var reg = new RegExp(”^[\u0391-\uFFE5]$”); |
6 | var result = 0; |
7 | for (i=0; i< valLength; i++) |
8 | { |
9 | if (reg.test(fData.charAt(i))) |
10 | { |
11 | result += 2; |
12 | } |
13 | else |
14 | { |
15 | result ++; |
16 | } |
17 | } |
18 | return result; |
19 | } |
http://ntt.cc/2008/05/10/over-10-useful-javascript-regular-expression-functions-to-improve-your-web-applications-efficiency.html
No comments:
Post a Comment