Uda*_*age 117 javascript string validation numbers
我的最终目标是验证输入字段.输入可以是字母或数字.
Zon*_*Zon 223
如果我没有弄错,问题需要"包含数字",而不是"数字".所以:
function hasNumber(myString) {
return /\d/.test(myString);
}
Run Code Online (Sandbox Code Playgroud)
Sta*_*arx 101
你可以用javascript做到这一点.不需要Jquery或Regex
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
Run Code Online (Sandbox Code Playgroud)
实施时
var val = $('yourinputelement').val();
if(isNumeric(val)) { alert('number'); }
else { alert('not number'); }
Run Code Online (Sandbox Code Playgroud)
更新:要检查字符串中是否包含数字,您可以使用正则表达式来执行此操作
var matches = val.match(/\d+/g);
if (matches != null) {
alert('number');
}
Run Code Online (Sandbox Code Playgroud)
Jis*_*A P 20
function validate(){
var re = /^[A-Za-z]+$/;
if(re.test(document.getElementById("textboxID").value))
alert('Valid Name.');
else
alert('Invalid Name.');
}
Run Code Online (Sandbox Code Playgroud)
小智 13
这就是你所需要的。
var hasNumber = /\d/;
hasNumber.test("ABC33SDF"); //true
hasNumber.test("ABCSDF"); //false
Run Code Online (Sandbox Code Playgroud)
在 JavaScript 中使用正则表达式。正则表达式是用于描述搜索模式的特殊文本字符串,它以 /pattern/modifiers 的形式编写,其中“pattern”是正则表达式本身,“modifiers”是表示各种选项的一系列字符。
该字符类是文字赛后最基本的正则表达式的概念。它使一个小的字符序列匹配一组更大的字符。例如,[A-Z]可以代表大写字母,\d可以代表任何数字。
从下面的例子
contains_alphaNumeric« 它检查字符串是否包含字母或数字(或)字母和数字。该连字符( - )将被忽略。onlyMixOfAlphaNumeric« 它检查字符串是否只包含任何序列顺序的字母和数字。例子:
function matchExpression( str ) {
var rgularExp = {
contains_alphaNumeric : /^(?!-)(?!.*-)[A-Za-z0-9-]+(?<!-)$/,
containsNumber : /\d+/,
containsAlphabet : /[a-zA-Z]/,
onlyLetters : /^[A-Za-z]+$/,
onlyNumbers : /^[0-9]+$/,
onlyMixOfAlphaNumeric : /^([0-9]+[a-zA-Z]+|[a-zA-Z]+[0-9]+)[0-9a-zA-Z]*$/
}
var expMatch = {};
expMatch.containsNumber = rgularExp.containsNumber.test(str);
expMatch.containsAlphabet = rgularExp.containsAlphabet.test(str);
expMatch.alphaNumeric = rgularExp.contains_alphaNumeric.test(str);
expMatch.onlyNumbers = rgularExp.onlyNumbers.test(str);
expMatch.onlyLetters = rgularExp.onlyLetters.test(str);
expMatch.mixOfAlphaNumeric = rgularExp.onlyMixOfAlphaNumeric.test(str);
return expMatch;
}
// HTML Element attribute's[id, name] with dynamic values.
var id1 = "Yash", id2="777", id3= "Yash777", id4= "Yash777Image4"
id11= "image5.64", id22= "55-5.6", id33= "image_Yash", id44= "image-Yash"
id12= "_-.";
console.log( "Only Letters:\n ", matchExpression(id1) );
console.log( "Only Numbers:\n ", matchExpression(id2) );
console.log( "Only Mix of Letters and Numbers:\n ", matchExpression(id3) );
console.log( "Only Mix of Letters and Numbers:\n ", matchExpression(id4) );
console.log( "Mixed with Special symbols" );
console.log( "Letters and Numbers :\n ", matchExpression(id11) );
console.log( "Numbers [-]:\n ", matchExpression(id22) );
console.log( "Letters :\n ", matchExpression(id33) );
console.log( "Letters [-]:\n ", matchExpression(id44) );
console.log( "Only Special symbols :\n ", matchExpression(id12) );
Run Code Online (Sandbox Code Playgroud)
输出:
Only Letters:
{containsNumber: false, containsAlphabet: true, alphaNumeric: true, onlyNumbers: false, onlyLetters: true, mixOfAlphaNumeric: false}
Only Numbers:
{containsNumber: true, containsAlphabet: false, alphaNumeric: true, onlyNumbers: true, onlyLetters: false, mixOfAlphaNumeric: false}
Only Mix of Letters and Numbers:
{containsNumber: true, containsAlphabet: true, alphaNumeric: true, onlyNumbers: false, onlyLetters: false, mixOfAlphaNumeric: true}
Only Mix of Letters and Numbers:
{containsNumber: true, containsAlphabet: true, alphaNumeric: true, onlyNumbers: false, onlyLetters: false, mixOfAlphaNumeric: true}
Mixed with Special symbols
Letters and Numbers :
{containsNumber: true, containsAlphabet: true, alphaNumeric: false, onlyNumbers: false, onlyLetters: false, mixOfAlphaNumeric: false}
Numbers [-]:
{containsNumber: true, containsAlphabet: false, alphaNumeric: false, onlyNumbers: false, onlyLetters: false, mixOfAlphaNumeric: false}
Letters :
{containsNumber: false, containsAlphabet: true, alphaNumeric: false, onlyNumbers: false, onlyLetters: false, mixOfAlphaNumeric: false}
Letters [-]:
{containsNumber: false, containsAlphabet: true, alphaNumeric: true, onlyNumbers: false, onlyLetters: false, mixOfAlphaNumeric: false}
Only Special symbols :
{containsNumber: false, containsAlphabet: false, alphaNumeric: false, onlyNumbers: false, onlyLetters: false, mixOfAlphaNumeric: false}
Run Code Online (Sandbox Code Playgroud)
它不是任何手段的防弹,但它适用于我的目的,也许它会帮助某人.
var value = $('input').val();
if(parseInt(value)) {
console.log(value+" is a number.");
}
else {
console.log(value+" is NaN.");
}
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以使用 JavaScript 来完成此操作。不需要 Jquery 或 Regex
function isNumeric(n)
{
return !isNaN(n);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
222714 次 |
| 最近记录: |