简体   繁体   English

使用isNumeric检查输入中的数字

[英]Checking for digits in an input with isNumeric

I am trying to check if there is at least 1 digit in an input field. 我正在尝试检查输入字段中是否至少有1位数字。 If there is I want to change the default image. 如果有,我想更改默认图像。

I am getting an error with the .isNumeric function. .isNumeric函数出现错误。 I know in the docs it shows $.isNumeric , but I am not sure how to add that within hasNumber.isNumeric().length >= 1; 我知道在文档中它显示$.isNumeric ,但是我不确定如何在hasNumber.isNumeric().length >= 1;添加它hasNumber.isNumeric().length >= 1; .

I am open to a different function if it will allow this to work. 我愿意接受其他功能,如果它可以使它起作用。

 $('#register').keyup(function() { var hasNumber = $("#password").val(); var hasNumberValid = hasNumber.isNumeric().length >= 1; $('#upperCase').attr('src', hasNumberValid ? 'https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQlyqJ14HYP1WclpK9RkJWo8jIDBkhTW0GS31AxRkozAEA72ULhY89LIzk' : 'icons/collection/delete.png'); }); 
 #password-check { margin: 30px auto; } .password-check-field { color: black; } .password-check-field img { margin-right: 15px; height: 15px; width: 15px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <form action="" method="POST" id="register"> <div class="field"> <label for="firstname">First Name</label> <input type="text" name="firstname" required> </div> <div class="field"> <label for="password">Choose a password</label> <input type="password" name="password" id="password" required> </div> <div class="password-check-field"> <img id="upperCase" src="icons/collection/delete.png" alt="Success">Your password has at a number in it </div> </form> 

$.isNumeric() accepts an argument of any value, so you can pass in your $("#password").val() to it via: $ .isNumeric()接受任何值的参数,因此您可以通过以下方式将$("#password").val()传递给它:

$.isNumeric(hasNumber) or $.isNumeric( $("#password").val() ) $.isNumeric(hasNumber)$.isNumeric( $("#password").val() )

The $.isNumeric() method checks whether its argument represents a numeric value. $.isNumeric()方法检查其参数是否表示数字值。 If so, it returns true . 如果是这样,则返回true Otherwise it returns false . 否则返回false The argument can be of any type. 参数可以是任何类型。

No need to use length on that one. 不需要在那一个上使用length

Try like this. 尝试这样。

 $('#register').keyup(function() { var hasNumber = $("#password").val(); var hasNumberValid = false; var inputArray = hasNumber.split(''); inputArray.forEach(function(element) { if($.isNumeric(element)) { hasNumberValid = true } }); $('#upperCase').attr('src', hasNumberValid ? 'https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQlyqJ14HYP1WclpK9RkJWo8jIDBkhTW0GS31AxRkozAEA72ULhY89LIzk' : 'icons/collection/delete.png'); }); 
 #password-check { margin: 30px auto; } .password-check-field { color: black; } .password-check-field img { margin-right: 15px; height: 15px; width: 15px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <form action="" method="POST" id="register"> <div class="field"> <label for="firstname">First Name</label> <input type="text" name="firstname" required> </div> <div class="field"> <label for="password">Choose a password</label> <input type="password" name="password" id="password" required> </div> <div class="password-check-field"> <img id="upperCase" src="icons/collection/delete.png" alt="Success">Your password has at a number in it </div> </form> 

isNumeric() returns a boolean. isNumeric()返回一个布尔值。 Check to see if it's true. 检查是否正确。 Try using $.isNumeric() 尝试使用$ .isNumeric()

var hasNumberValid = $.isNumeric(hasNumber)

您可以使用简单的正则表达式来检查字符串中是否有数字

var hasNumberValid = hasNumber.match(/\d+/g) != null;

I would put the keyup on your password field instead of the form and use a regular expression instead of isNumericn since the documentation states that "Description: Determines whether its argument represents a JavaScript number." 我将把keyup放在密码字段而不是表单上,并使用正则表达式而不是isNumericn,因为文档指出“ Description:确定其参数是否表示JavaScript数字”。 I'm not sure whether it will be true in the case where you have both numbers and letters. 我不确定在您同时拥有数字和字母的情况下是否正确。

fiddle example 小提琴的例子

var AtLestOneLetterAndOneNumberRegex = new RegExp(".*[0-9].*");
$('#password').keyup(function() {
  var passwordValue = $("#password").val();
  if (AtLestOneLetterAndOneNumberRegex.test(passwordValue)){

    $("#validPasswordMessage").css("visibility","visible");
  }

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM