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

 In a previous post trim a string from white space by JavaScript function ( or Another JavaScript function for trim a string from white space ) and JavaScript function-Splits the string by given separator and returns an array with trimmed items, Some Regular expressions have been used. Regular expressions are very powerful tools for performing pattern matches.And validating user input is the bane of every software developer existence. But these patterns used in RegExp can be very simple. 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:
  1. var 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
  2. var isNonblank_re    = //S/;
  3. function 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).
  2. var isWhole_re       = /^/s*/d+/s*$/;
  3. function isWhole (s) {
  4.    return String(s).search (isWhole_re) != -1
  5. }

or as seen in the following snippet:

  1. // check 0-9 digit
  2. function 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.
  2. var isInteger_re     = /^/s*(/+|-)?/d+/s*$/;
  3. function isInteger (s) {
  4.    return String(s).search (isInteger_re) != -1
  5. }

or as seen in the following snippet:

  1. // check is number
  2. function 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.
  2. var isDecimal_re     = /^/s*(/+|-)?((/d+(/./d+)?)|(/./d+))/s*$/;
  3. function 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
  2. var isCurrency_re    = /^/s*(/+|-)?((/d+(/./d/d)?)|(/./d/d))/s*$/;
  3. function 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.
  2. var isEmail_re       = /^/s*[/w/-/+_]+(/.[/w/-/+_]+)*/@[/w/-/+_]+/.[/w/-/+_]+(/.[/w/-/+_]+)*/s*$/;
  3. function 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
  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.   }

No.7: Check for valid credit card type/number

  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. }

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.

  1. function luhn (cc) {
  2.    var sum = 0;
  3.    var i;
  4.    for (i = cc.length - 2; i >= 0; i -= 2) {
  5.       sum += Array (0, 2, 4, 6, 8, 1, 3, 5, 7, 9) [parseInt (cc.charAt (i), 10)];
  6.    }
  7.    for (i = cc.length - 1; i >= 0; i -= 2) {
  8.       sum += parseInt (cc.charAt (i), 10);
  9.    }
  10.    return (sum % 10) == 0;
  11. }

No.8: Returns a string with everything but the digits removed

  1. // This returns a string with everything but the digits removed.
  2. function getdigits (s) {
  3.    return s.replace (/[^/d]/g, “”);
  4. }

No.9: Get String Length
// Get String Length

  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. }

No.10: Trim a string from white space
Trim a string from white space

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值