簡體   English   中英

使用isNumeric檢查輸入中的數字

[英]Checking for digits in an input with isNumeric

我正在嘗試檢查輸入字段中是否至少有1位數字。 如果有,我想更改默認圖像。

.isNumeric函數出現錯誤。 我知道在文檔中它顯示$.isNumeric ,但是我不確定如何在hasNumber.isNumeric().length >= 1;添加它hasNumber.isNumeric().length >= 1;

我願意接受其他功能,如果它可以使它起作用。

 $('#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()接受任何值的參數,因此您可以通過以下方式將$("#password").val()傳遞給它:

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

$.isNumeric()方法檢查其參數是否表示數字值。 如果是這樣,則返回true 否則返回false 參數可以是任何類型。

不需要在那一個上使用length

嘗試這樣。

 $('#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()返回一個布爾值。 檢查是否正確。 嘗試使用$ .isNumeric()

var hasNumberValid = $.isNumeric(hasNumber)

您可以使用簡單的正則表達式來檢查字符串中是否有數字

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

我將把keyup放在密碼字段而不是表單上,並使用正則表達式而不是isNumericn,因為文檔指出“ Description:確定其參數是否表示JavaScript數字”。 我不確定在您同時擁有數字和字母的情況下是否正確。

小提琴的例子

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