Tuesday, February 5, 2013

10+ Useful JavaScript Regular Expression Functions to improve your web applications efficiency


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 /abc/, the syntax for that method is the following:
1var RegularExpression = /pattern/[switch]
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
1// Check if string is non-blank
2var isNonblank_re    = /\S/;
3function isNonblank (s) {
4   return String (s).search (isNonblank_re) != -1
5}
No.2: Check if string is a whole number(digits only).
1// Check if string is a whole number(digits only).
2var isWhole_re       = /^\s*\d+\s*$/;
3function isWhole (s) {
4   return String(s).search (isWhole_re) != -1
5}
or as seen in the following snippet:
1// check 0-9 digit
2function regIsDigit(fData)
3{
4    var reg = new RegExp(”^[0-9]$”);
5    return (reg.test(fData));
6}
No.3: Checks that an input string is an integer
1// checks that an input string is an integer, with an optional +/- sign character.
2var isInteger_re     = /^\s*(\+|-)?\d+\s*$/;
3function isInteger (s) {
4   return String(s).search (isInteger_re) != -1
5}
or as seen in the following snippet:
1// check is number
2function regIsNumber(fData)
3{
4    var reg = new RegExp(”^[-]?[0-9]+[\.]?[0-9]+$”);
5    return reg.test(fData)
6}
No.4: Checks that an input string is a decimal number
1// Checks that an input string is a decimal number, with an optional +/- sign character.
2var isDecimal_re     = /^\s*(\+|-)?((\d+(\.\d+)?)|(\.\d+))\s*$/;
3function isDecimal (s) {
4   return String(s).search (isDecimal_re) != -1
5}
No.5: Check if string is currency
works just like isDecimal, except that only zero or two digits are allowed after the decimal point.
1// Check if string is currency
2var isCurrency_re    = /^\s*(\+|-)?((\d+(\.\d\d)?)|(\.\d\d))\s*$/;
3function isCurrency (s) {
4   return String(s).search (isCurrency_re) != -1
5}
No.6: Checks that an input string looks like a valid email address
1// checks that an input string looks like a valid email address.
2var isEmail_re       = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;
3function isEmail (s) {
4   return String(s).search (isEmail_re) != -1;
5}
or as seen in the following snippet:
1// Check if string is a valid email address
2function 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  }
No.7: Check for valid credit card type/number
1// Check for valid credit card type/number
2var 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];
15function 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}
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.
1function 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}
No.8: Returns a string with everything but the digits removed
1// This returns a string with everything but the digits removed.
2function getdigits (s) {
3   return s.replace (/[^\d]/g, “”);
4}
No.9: Get String Length // Get String Length
1// Get String Length
2function 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