提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
实用简单JS代码(チェックについて)
`提示:暂时这些,以后遇到会再加。
一、チェック
1.全部全角チェック
代码如下(示例):
function checkFull(data) {
for(var i = 0; i < data.length; i++){
var data_unicode = data.charCodeAt(i);
if(data_unicode < 256 || (data_unicode >= 0xff61 && data_unicode <= 0xff9f)){
return false;
}
}
return true;
}
2.電話チェック
代码如下(示例):
function isPhone(strNum){
var Letters="1234567890-";
var i;
var c;
if(strNum.charAt(0)=='-')
return false;
if(strNum.charAt(strNum.length-1)=='-')
return false;
for(i=0;i < strNum.length; i++){
c = strNum.charAt(i);
if(Letters.indexOf(c)<0)
return false;
}
return true;
}
3.お金チェック
代码如下(示例):
function isMoney(strNum){
var Letters="1234567890,";
var i;
var c;
if(strNum.charAt(0)==',')
return false;
if(strNum.charAt(strNum.length-1)==',')
return false;
for(i=0;i < strNum.length; i++){
c = strNum.charAt(i);
if(Letters.indexOf(c)<0)
return false;
}
return true;
}
4.Emailチェック
代码如下(示例):
function isEmail(strEmail) {
if (strEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
return true;
else
return false;
}
5.数字チェック
代码如下(示例):
function onlyNum(){
if(!((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105)))
event.returnvalue=false;
return true;
}
6.全部半角チェック
代码如下(示例):
function checkHan(data) {
for(var i = 0; i < data.length; i++){
var data_unicode = data.charCodeAt(i);
if(data_unicode < 256 || (data_unicode >= 0xff61 && data_unicode <= 0xff9f)){
} else {
return false;
}
}
return true;
}
7.全角カナチェック
代码如下(示例):
function checkKana(str){
for(var i = 0; i < str.length; i++){
if(str.charCodeAt(i) < 0x30a1 || str.charCodeAt(i) > 0x30f6){
return false;
}
}
return true;
}
8.日期チェック
代码如下(示例):
ffunction checkDate(dateStr) {
if(!dateStr){
return false;
}
if ( dateStr.length != 10 ) {
return false;
}
var splitStr = dateStr.split("/");
if ( splitStr.length != 3 ) {
return false;
}
var year = splitStr[0];
var month = splitStr[1];
var day = splitStr[2];
if ( !isNumber(year) || !isNumber(month) || !isNumber(day) ) {
return false;
}
if ( year.length!=4 || parseInt(year) < 1 || parseInt > 9999 ) {
return false;
}
if ( month.length!=2 || parseInt(month,10)<1 || parseInt(month,10)>12 ) {
return false;
}
if ( day.length!=2 || parseInt(day,10) <1 || parseInt(day,10) > 31) {
return false;
}
var dayCount = fnComputerDay(parseInt(year,10), parseInt(month,10));
if ( parseInt(dayCount,10) < parseInt(day,10) ) {
return false;
}
return true;
}
function fnComputerDay(intYear,intMonth) {
var dtmDate = new Date(intYear,intMonth,-1);
var intDay = dtmDate.getDate() + 1;
return intDay;
}
function isNumber( str ) {
if ( !str || str==null || str == "") {
return false;
}
if ((isNaN(str)) || (str.indexOf(".")!=-1) || (str.indexOf("-")!=-1)){
return false ;
}
return true;
}
9.お金のフォーマット
代码如下(示例):
function formatMoney(s,type){
if(s==null || s=='') return "0";
do{
s = s.replace(",","");
}while(s.indexOf(",")!=-1);
if(/[^0-9\.]/.test(s))return "0";
s=s.toString().replace(/^(\d*)$/,"$1.");
s=(s+"00").replace(/(\d*\.\d\d)\d*/,"$1");
s=s.replace(".",",");
var re = /(\d)(\d{3},)/;
while(re.test(s)){
s = s.replace(re,"$1,$2");
}
s = s.replace(/,(\d\d)$/,".$1");
if(type == 0){
var a = s.split(".");
s = a[0];
}
return s;
}
该处使用的url网络请求的数据。