我的最终目标是验证输入字段。输入可以是字母也可以是数字。
当前回答
下面的代码检查相同的数字,序列号和反向的数字序列。
function checkNumSequnce(arrayNM2) {
inseqCounter=1;
continousSeq = 1;
decsequenceConter = 1;
var isequence = true;
for (i=0;i<arrayNM2.length-1;i++) {
j=i+1;
if (arrayNM2[i] == arrayNM2[i+1]) {
if(inseqCounter > 1 || decsequenceConter > 1){
isequence = false; break;
}
continousSeq++;
}
else if (arrayNM2[j]- arrayNM2[i] == 1) {
if(decsequenceConter > 1 || continousSeq > 1){
isequence = false; break;
}
inseqCounter++;
} else if(arrayNM2[i]- arrayNM2[j] == 1){
if(inseqCounter > 1 || continousSeq > 1){
isequence = false; break;
}
decsequenceConter++;
}else{
isequence= false;
break;
}
};
console.log("isequence: "+ isequence);
};
其他回答
如果我没记错的话,这个问题需要“contains number”,而不是“is number”。所以:
function hasNumber(myString) {
return /\d/.test(myString);
}
你可以使用javascript来做到这一点。不需要Jquery或Regex
function isNumeric(n)
{
return !isNaN(n);
}
Using Regular Expressions with JavaScript. A regular expression is a special text string for describing a search pattern, which is written in the form of /pattern/modifiers where "pattern" is the regular expression itself, and "modifiers" are a series of characters indicating various options. The character class is the most basic regex concept after a literal match. It makes one small sequence of characters match a larger set of characters. For example, [A-Z] could stand for the upper case alphabet, and \d could mean any digit.
从下面的例子
contains_alphaNumeric«它检查字符串是否包含字母或数字(或)字母和数字。连字符(-)将被忽略。 它检查字符串是否只包含任意序列顺序的字母和数字。
例子:
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) );
把:
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}
java正则表达式模式匹配。
无论如何,它都不是刀枪不入的,但它对我来说是有效的,也许它会帮助到别人。
var value = $('input').val();
if(parseInt(value)) {
console.log(value+" is a number.");
}
else {
console.log(value+" is NaN.");
}
我们可以使用!/[^a-zA-Z]/.test(e)来检查它。 只需运行snippet并检查。
handleValueChange() { 如果(! / [^ a-zA-Z] / test (. getelementbyid(“textbox_id”)value)) { var x = document.getElementById('result'); x.innerHTML = '字符串不包含数字'; }其他{ var x = document.getElementById('result'); x.innerHTML = 'String does contains number'; } } 输入{ 填充:5 px; } <input type="text" id="textbox_id" placeholder="Enter string here…" oninput="handleValueChange()" > < p id = "结果" > < / p >
推荐文章
- 如何使用Jest测试对象键和值是否相等?
- 将长模板文字行换行为多行,而无需在字符串中创建新行
- 如何在JavaScript中映射/减少/过滤一个集?
- Bower: ENOGIT Git未安装或不在PATH中
- 是否有可能更新一个本地化的故事板的字符串?
- 为什么字符串类型的默认值是null而不是空字符串?
- 添加javascript选项选择
- 在Node.js中克隆对象
- 在Python中包装长行
- 为什么在JavaScript的Date构造函数中month参数的范围从0到11 ?
- 使用JavaScript更改URL参数并指定默认值
- jQuery:什么是限制“数字”仅输入文本框的最佳方法?(允许使用小数点)
- 在window.setTimeout()发生之前取消/终止
- 如何删除未定义和空值从一个对象使用lodash?
- 如何在AngularJS中有条件地要求表单输入?