我的最终目标是验证输入字段。输入可以是字母或数字。
如果我没记错的话,这个问题需要“包含数字”,而不是“是数字”。所以:
function hasNumber(myString) {
return /\d/.test(myString);
}
您可以使用javascript执行此操作。无需Jquery或Regex
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
在实施时
var val = $('yourinputelement').val();
if(isNumeric(val)) { alert('number'); }
else { alert('not number'); }
更新:要检查字符串中是否包含数字,可以使用正则表达式来执行此操作
var matches = val.match(/\d+/g);
if (matches != null) {
alert('number');
}
function validate(){
var re = /^[A-Za-z]+$/;
if(re.test(document.getElementById("textboxID").value))
alert('Valid Name.');
else
alert('Invalid Name.');
}
它绝不是防弹的,但它可以达到我的目的,也许会对某人有所帮助。
var value = $('input').val();
if(parseInt(value)) {
console.log(value+" is a number.");
}
else {
console.log(value+" is NaN.");
}
在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) );
输出:
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}
测试是否有任何字符是数字,且不需过多改写,以根据需要进行调整。
const s = "EMA618"
function hasInt(me){
let i = 1,a = me.split(""),b = "",c = "";
a.forEach(function(e){
if (!isNaN(e)){
console.log(`CONTAIN NUMBER «${e}» AT POSITION ${a.indexOf(e)} => TOTAL COUNT ${i}`)
c += e
i++
} else {b += e}
})
console.log(`STRING IS «${b}», NUMBER IS «${c}»`)
if (i === 0){
return false
// return b
} else {
return true
// return +c
}
}
hasInt(s)
这就是你所需要的。
var hasNumber = /\d/;
hasNumber.test("ABC33SDF"); //true
hasNumber.test("ABCSDF"); //false
一种检查方法是遍历字符串并在击中数字时返回true(或false,具体取决于您想要的)。
function checkStringForNumbers(input){
let str = String(input);
for( let i = 0; i < str.length; i++){
console.log(str.charAt(i));
if(!isNaN(str.charAt(i))){ //if the string is a number, do the following
return true;
}
}
}
parseInt
当字符串以整数表示形式开头时,提供整数:
(parseInt '1a') is 1
..所以也许:
isInteger = (s)->
s is (parseInt s).toString() and s isnt 'NaN'
(isInteger 'a') is false
(isInteger '1a') is false
(isInteger 'NaN') is false
(isInteger '-42') is true
请原谅我的CoffeeScript。
您可以使用javascript执行此操作。无需Jquery或Regex
function isNumeric(n)
{
return !isNaN(n);
}
当发现数字停止执行时,此代码还有助于“检测给定字符串中的数字”。
function hasDigitFind(_str_) {
this._code_ = 10; /*When empty string found*/
var _strArray = [];
if (_str_ !== '' || _str_ !== undefined || _str_ !== null) {
_strArray = _str_.split('');
for(var i = 0; i < _strArray.length; i++) {
if(!isNaN(parseInt(_strArray[i]))) {
this._code_ = -1;
break;
} else {
this._code_ = 1;
}
}
}
return this._code_;
}
您也可以尝试lodash:
const isNumeric = number =>
_.isFinite(_.parseInt(number)) && !_.isNaN(_.parseInt(number))
本文地址:http://javascript.askforanswer.com/jianchashuruzifuchuanshifouzaijavascriptzhongbaohanshuzi.html
文章标签:javascript , numbers , string , validation
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
文章标签:javascript , numbers , string , validation
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
评论已关闭!