您的位置:首页 > 移动开发

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

2009-09-09 17:47 381 查看
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:
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

// Check if string is non-blank
var isNonblank_re = //S/;
function isNonblank (s) {
return String (s).search (isNonblank_re) != -1
}

No.2: Check if string is a whole number(digits only).

// Check if string is a whole number(digits only).
var isWhole_re = /^/s*/d+/s*$/;
function isWhole (s) {
return String(s).search (isWhole_re) != -1
}
or as seen in the following snippet:

// check 0-9 digit
function regIsDigit(fData)
{
var reg = new RegExp(”^[0-9]$”);
return (reg.test(fData));
}

No.3: Checks that an input string is an integer

// checks that an input string is an integer, with an optional +/- sign character.
var isInteger_re = /^/s*(/+|-)?/d+/s*$/;
function isInteger (s) {
return String(s).search (isInteger_re) != -1
}
or as seen in the following snippet:

// check is number
function regIsNumber(fData)
{
var reg = new RegExp(”^[-]?[0-9]+[/.]?[0-9]+$”);
return reg.test(fData)
}

No.4: Checks that an input string is a decimal number

// Checks that an input string is a decimal number, with an optional +/- sign character.
var isDecimal_re = /^/s*(/+|-)?((/d+(/./d+)?)|(/./d+))/s*$/;
function isDecimal (s) {
return String(s).search (isDecimal_re) != -1
}

No.5: Check if string is currency

works just like isDecimal, except that only zero or two digits are allowed after the decimal point.

// Check if string is currency
var isCurrency_re = /^/s*(/+|-)?((/d+(/./d/d)?)|(/./d/d))/s*$/;
function isCurrency (s) {
return String(s).search (isCurrency_re) != -1
}

No.6: Checks that an input string looks like a valid email address

// checks that an input string looks like a valid email address.
var isEmail_re = /^/s*[/w/-/+_]+(/.[/w/-/+_]+)*/@[/w/-/+_]+/.[/w/-/+_]+(/.[/w/-/+_]+)*/s*$/;
function isEmail (s) {
return String(s).search (isEmail_re) != -1;
}
or as seen in the following snippet:

// Check if string is a valid email address
function regIsEmail(fData)
{
var reg = new RegExp(”^[0-9a-zA-Z]+@[0-9a-zA-Z]+[/.]{1}[0-9a-zA-Z]+[/.]?[0-9a-zA-Z]+$”);
return reg.test(fData);
}

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

// Check for valid credit card type/number
var creditCardList = [
//type prefix length
["amex", "34", 15],
["amex", "37", 15],
["disc", "6011", 16],
["mc", "51", 16],
["mc", "52", 16],
["mc", "53", 16],
["mc", "54", 16],
["mc", "55", 16],
["visa", "4", 13],
["visa", "4", 16]
];
function isValidCC (cctype, ccnumber) {
var cc = getdigits (ccnumber);
if (luhn (cc)) {
for (var i in creditCardList) {
if (creditCardList [i][0] == (cctype.toLowerCase ())) {
if (cc.indexOf (creditCardList [i][1]) == 0) {
if (creditCardList [i][2] == cc.length) {
return true;
}
}
}
}
}
return false;
}
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.

function luhn (cc) {
var sum = 0;
var i;

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

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

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

No.9: Get String Length

// Get String Length

// Get String Length
function regGetStrLength(fData)
{
var valLength = fData.length;
var reg = new RegExp(”^[/u0391-/uFFE5]$”);
var result = 0;
for(i=0; i< valLength; i++)
{
if(reg.test(fData.charAt(i)))
{
result += 2;
}
else
{
result ++;
}
}
return result;
}

No.10: Trim a string from white space

Trim a string from white space

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/heimaoxiaozi/archive/2008/11/06/3238952.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: