[英]Max and Min for an Input
對於此特定的現有表格,有一個輸入來接受電話號碼。 它已經過驗證,只接受數字,並為10個字符添加了最大字符屬性。
但是,有人可以添加1-9位數字。 因此,我需要添加JavaScript來檢查以確保該字段的字符數為10。輸入的ID為電話號碼。
有人可以告訴我如何修改以下代碼以使其正常工作嗎? 注意:“ = 10個字符”只是一個占位符,該部分需要用實際代碼替換。
function checkEmail(theForm) {
if (theForm.getElementById("phonenumber").value = 10 Characters )
{
alert('Your phone number is not 10 digits.');
return false;
} else {
return true;
}
}
我想你想要.length
if (theForm.getElementById("phonenumber").value.length == 10) {
您可能希望對用戶保持謙虛,並允許電話號碼使用常規約定,例如空格或連字符。
只需檢查10位數字。 使用該值時,請刪除所有非數字。
function checkphone(v){
if(v.match(/\d/g).length==10) return true;
throw 'Phone number must have 10 digits';
}
支票電話('207 555-5555');
if (theForm.getElementById("phonenumber").value.length != 10)
...因為長度不等於10時您希望發生某些情況。
使用Javascript驗證來驗證電話號碼的最小和最大長度
function checkLimit() { var x, text; // Get the value of the input field with id="Phone" phone = document.getElementById("Phone").value; // If phone is Not a Number or less than 10 or greater than 10 if (isNaN(phone) || phone.length < 10 || phone.length > 10) { text = "Input not valid"; } else { text = "Input OK"; } document.getElementById("resp").innerHTML = text; }
<input onkeypress="checkLimit()" id="Phone" name="Phone" type="number" class="form-control" placeholder="Phone Number" required="required"/> <p id="resp"></p>
嘗試這個:
function checkEmail(theForm) {
if (theForm.getElementById("phonenumber").value.length != 10 )
{
alert('Your phone number is not 10 digits.');
return false;
} else {
return true;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.