使用正則表達式:
/^-?([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