10+ Useful JavaScript Regular Expression Functions to improve your web applications efficiency
来源:互联网 发布:js 不等于 编辑:程序博客网 时间:2024/06/01 23:31
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
- 10+ Useful JavaScript Regular Expression Functions to improve your web applications efficiency
- 10+ Useful JavaScript Regular Expression Functions to improve your web applications efficiency
- [R] R functions to deal with regular expression
- Using AJAX to Improve the Bandwidth Performance of Web Applications
- Ten ways to improve testing, performance of Web 2.0 applications
- Improve efficiency
- 10 ways to Improve Your Programming Productivity
- 10 ways to improve your programming skills
- 10 Tips to Improve an ASP.NET Applications Performance
- Regular Expression in JavaScript
- How To Improve Your English
- how to improve your eyesight
- To improve your programming skills
- 10 Specific Ways to Improve Your Productivity With Emacs
- 10 Specific Ways to Improve Your Productivity With Emacs
- 10 Tips to improve your Software Architecture Design
- 10 Ways To Improve Your Indie Game Development
- Improve Work Efficiency
- set default path of linux on msst.cc
- C++类对象的拷贝构造函数
- Erlang 环形基准测试程序
- 各种技术网站的网址
- Silverlight中引用wcf出现“给定关键字不在字典中”的错误
- 10+ Useful JavaScript Regular Expression Functions to improve your web applications efficiency
- 第10章 关联容器
- 深入浅出Attribute
- C#的多线程机制探索
- 常用正则表达式大全!(例如:匹配中文、匹配html)
- [原创] 活力(智力情绪体力)周期测试脚本 (bash脚本)
- 「Birt」BIRT使用API进行简单部署
- story for thinking
- 很好算法BLOG