Home >Web Front-end >JS Tutorial >JavaScript Advanced (3) Common Tools (Validation, General)

JavaScript Advanced (3) Common Tools (Validation, General)

黄舟
黄舟Original
2017-02-11 14:40:511520browse

JS common tools (verification, general)

// Name verification

var checkName = function(name)
{
// 收货人姓名校验(准则:姓名为2-4汉字)
var regu = /^[\u4E00-\u9FA5]{2,4}$/;
var re = new RegExp(regu);
if (!re.test(name)) {
return false;
}
 
return true;
};

//Mobile phone number verification Verification

var checkCellphone = function(cellPhone)
{
var regu =  /^[S|U]((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,2,3,5-9]))\d{8}$/;
var re = new RegExp(regu);
if (!re.test(cellPhone)) {
return false;
}
return true;
};

// Date format conversion

var formatDateTime = function (date)
{ 
if(date == null){
return null;
}else{
var y = date.getFullYear();  
var m = date.getMonth() + 1;  
m = m < 10 ? (&#39;0&#39; + m) : m;  
var d = date.getDate();  
d = d < 10 ? (&#39;0&#39; + d) : d;  
var h = date.getHours();  
var minute = date.getMinutes();  
minute = minute < 10 ? (&#39;0&#39; + minute) : minute;  
var second = date.getSeconds();
return y + &#39;-&#39; + m + &#39;-&#39; + d +&#39; &#39; + h + &#39;:&#39;+minute+&#39;:&#39;+second; 
}
};

// Get the current time, the format is: YYYY-MM-DD

var CurentTime = function()  
{   
    var now = new Date();  
   
    var year = now.getFullYear();       //年  
    var month = now.getMonth() + 1;     //月  
    var day = now.getDate();            //日  
         
    var clock = year + "";  
        
    if(month < 10) clock += "0";         
    clock += month + "";  
         
    if(day < 10) clock += "0";   
    clock += day + "";  
  
    return(clock);   
};

//Verification password format

var checkPasswd = function(passwd)
{
var myreg = /^(\w|[a-z]){6,9}$/;
var re = new RegExp(myreg);
if(!re.test(passwd))
{
    return false;
}
return true;
};

The above are JavaScript advanced (3) common tools (verification , general) content, for more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn