使用正则表达式:
/^-?([1-9]\d+|\d)(\.\d+)?$/.test("1234"); // true
/^-?([1-9]\d+|\d)(\.\d+)?$/.test("asdf"); // false
/^[a-zA-Z]+$/.test("asdf"); // true
/^[a-zA-Z]+$/.test("1234"); // false
,或者你只是想用相同的名称,PHP两个功能:
function ctype_alpha(input) {
// this works for both upper and lower case
return /^[a-zA-Z]+$/.test(input);
}
function is_numeric(input) {
// this works for integer, float, negative and positive number
return /^-?([1-9]\d+|\d)(\.\d+)?$/.test(input);
}
ctype_alpha("asdf"); // true
is_numeric("1234"); // true
is_numeric("-1234"); // true
is_numeric("12.34"); // true
is_numeric("0.4"); // true
is_numeric("001"); // false
所以最后一个端口,你的代码使用的JS:
var input = "your_string"
function ctype_alpha(input) {
// this works for both upper and lower case
return /^[a-zA-Z]+$/.test(input);
}
function is_numeric(input) {
// this works for integer, float, negative and positive number
return /^-?([1-9]\d+|\d)(\.\d+)?$/.test(input);
}
if(ctype_alpha(input.substring(0, 2)) && is_numeric(input.substring(2, 4))) {
//do smthing
}
来源
2014-12-05 13:05:08
Leo
这只适用于整个正数,例如:'/^\d+$/.test("1.234“); // false /^\d+$/.test("-1234“); // false' – 2014-12-05 13:09:13
这取决于提问者的要求。就目前而言,他并没有说太多。而我只是在给出一个线索,而不是完成他的工作。 – Leo 2014-12-05 13:12:20
我已经说过,我需要javascript ... – 2014-12-05 13:15:39