简体   繁体   English

输入的最大值和最小值

[英]Max and Min for an Input

For this particular existing form there is an input to accept phone numbers. 对于此特定的现有表格,有一个输入来接受电话号码。 It already validates to only accept numbers and added a max character attribute for 10 characters. 它已经过验证,只接受数字,并为10个字符添加了最大字符属性。

However someone could add 1-9 digits. 但是,有人可以添加1-9位数字。 So I need to add javascript to check to make sure the character count for that field is 10. The input has an ID of phonenumber. 因此,我需要添加JavaScript来检查以确保该字段的字符数为10。输入的ID为电话号码。

Can someone tell me how to modify the following code to make that work? 有人可以告诉我如何修改以下代码以使其正常工作吗? Note: The "= 10 Characters" is just a placeholder, that part needs to be replaced with real code. 注意:“ = 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) { 

You may want to be gentle with your users, and allow common conventions in phone numbers, like spaces or dash-hyphens. 您可能希望对用户保持谦虚,并允许电话号码使用常规约定,例如空格或连字符。

Just check for 10 digits. 只需检查10位数字。 When you use the value, remove any non digits. 使用该值时,请删除所有非数字。

function checkphone(v){
    if(v.match(/\d/g).length==10) return true;
    throw 'Phone number must have 10 digits';
}

checkphone('207 555-5555'); 支票电话('207 555-5555');

if (theForm.getElementById("phonenumber").value.length != 10)

...因为长度等于10时您希望发生某些情况。

Verify min and max length of phone number field using Javascript Validation 使用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> 

try this: 尝试这个:

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.

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