2016-12-16 18 views
0

我正在檢查輸入字段中是否至少有一位數字。如果有我想更改默認圖像。使用isNumeric檢查輸入中的數字

我收到.isNumeric函數的錯誤。我知道在文檔中顯示$.isNumeric,但我不知道如何在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>

+3

嘗試'$ .isNumeric(hasNumber)'' – j08691

+1

變種ISNUMERIC = $ .isNumeric(value_to_check);' –

+1

https://api.jquery.com/jQuery.isNumeric/。嘗試'$ .isNumeric(hasNumber)' –

回答

2

$.isNumeric()接受任何值的參數,這樣你就可以在你的$("#password").val()通過給它:

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

$.isNumeric()方法檢查它的參數是否代表一個 數值。如果是,則返回true。否則它返回false。參數 可以是任何類型。

不需要使用length那個。

0

isNumeric()返回一個布爾值。檢查是否是真的。嘗試使用$.isNumeric()

var hasNumberValid = $.isNumeric(hasNumber) 
0

您可以使用一個簡單的正則表達式,以檢查是否有在你的字符串一些

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

嘗試這樣的。

$('#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>

0

我會把你的密碼字段KEYUP,而不是形式的,並且由於文件指出,「說明使用正則表達式,而不是isNumericn:確定其參數是否代表JavaScript數字「。我不確定在你有數字和字母的情況下它是否會成立。

fiddle example

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

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

}); 
+0

我沒有重命名長變量,我很抱歉,這只是檢查一個數字,它不檢查字母 – aemorales1