简体   繁体   English

确保用户以JavaScript格式提交的邮政编码不能少于5个数字

[英]Making sure that a user can't submit less than 5 numbers for zip code in a JavaScript form

I am building a registration form and I need a way of preventing users from submitting less than 5 numbers for the zip code. 我正在建立注册表,并且需要一种防止用户提交少于5个邮政编码的方法。 When I comment out my attempt for the else if entirely, the other function (making sure that users won't submit any letters) works. 当我完全注释掉其他选项的尝试时,其他功能(确保用户不会提交任何字母)起作用。 I am using return false at the end so that the form won't submit and I can test for bad input. 我在最后使用return false,以便表单不会提交,并且我可以测试错误的输入。 Sorry for the lack of jsfiddle - it wouldn't let me submit forms for some reason (I am a noob, so that may be the reason). 很抱歉缺少jsfiddle-出于某种原因,它不会让我提交表单(我是菜鸟,所以可能是原因)。 Here's a snippet of my code: 这是我的代码片段:

HTML: HTML:

<form name="reg" onSubmit="return formValidation();">    
<p>Zip Code: <input type="text" id="zip" maxlength="5" name="zip"></p>
<input type="submit" id="submitbutton" name="submitbutton" value="Submit" />
<div id="msg"></div> 
</form>

JS: JS:

function formValidation()  
{  
    var zip = document.reg.zip; 
    allnumeric(zip);

function allnumeric(zip)
{   
    var numbers = /^[0-9]+$/;  
    if(zip.value.match(numbers))  
    {  
        document.getElementById("msg").innerHTML=("OK &#x2713;");
        msg.style.color="green";
    }  
    else if (zip.numbers < 5) /*DOES NOT COMPUTE*/
    {
        msg.innerHTML=("Has to be 5 numbers.");
        msg.style.color="red";
    }
    else
    {  
        msg.innerHTML=("Numbers only please.");
        msg.style.color="red";
    }  
};  
    return false;
};

I'd suggest embracing HTML5 我建议拥抱HTML5

<form name="reg" onSubmit="return formValidation();">
    <p>Zip Code:
        <input type="text" id="zip" pattern="\d{5}" maxlength="5" name="zip" required="required" />
    </p>
    <input type="submit" id="submitbutton" name="submitbutton" value="Submit" />
    <div id="msg"></div>
</form>

FIDDLE 小提琴

You want zip.value.length , not zip.numbers . 您需要zip.value.length ,而不是zip.numbers

You can also do the whole thing with the regex: 您还可以使用正则表达式完成整个操作:

var numbers = /^\d{5}$/;  
if(zip.value.match(numbers))  
{  
    document.getElementById("msg").innerHTML=("OK &#x2713;");
    msg.style.color="green";
}  
else
{  
    msg.innerHTML=("Numbers only please.");
    msg.style.color="red";
} 

I think the other answers cover the essence of your JS question but not really the Data Quality aspect as I don't think you really should be validating a zip in this way. 我认为其他答案涵盖了您的JS问题的本质,但并不涉及数据质量方面,因为我认为您实际上不应该以这种方式验证zip。

You're certainly taking the correct approach by trying to ensure the quality of the user input - but just checking for 5 numbers might not be enough. 您肯定会通过尝试正确的方法来尝试确保用户输入的质量-但仅检查5个数字可能还不够。

There of tens of thousands of addresses where a 4 or 3 digit zip code should be considered valid input - where the Zip starts with one or two 0's. 在成千上万的地址中,应将4或3位数的邮政编码视为有效输入-邮政编码以一或两个0开头。 Some examples include: 一些示例包括:

  • Bridgeton, NJ 08302 新泽西州布里奇顿08302
  • Holtsville, NY 00544 纽约州霍尔茨维尔00544
  • Adjuntas, PR 00601 (All zip codes in Puerto Rico start with 00) Adjuntas,PR 00601(波多黎各的所有邮政编码均以00开头)
  • Christiansted, VI 00821 克里斯琴斯特德(VI)00821

Users frequently omit the leading 00 / 0 and that should really be allowed in your form - they may find it an unpleasant distraction from your workflow if users are getting the handy inline validation message telling them they are incorrect when they know they are not. 用户经常忽略前导00/0,这在您的表单中实际上是应允许的-如果用户收到方便的内嵌验证消息,告诉他们在不知道的情况下是不正确的,则可能会使您的工作流不愉快。

The second possible flaw (again depending upon your workflow) is that people may enter the zip+4 too. 第二个可能的缺陷(同样取决于您的工作流程)是人们也可能会输入zip + 4 You probably don't want to limit them to just the 5 when you may be able to get the full 9. 当您可能会获得完整的9时,您可能不想将它们限制为仅5。

This means that your JS test would have to be updated to allow for the combinations, but would lead to a more robust final product. 这意味着您的JS测试必须进行更新以允许组合使用,但最终产品会更可靠。 At a minimum, I'd suggest that as part of your form you include example zip codes (placeholder text or similar) which include the '00' case so users know what is expected. 至少,我建议在表单的一部分中包含示例邮政编码(占位符文本或类似内容),其中包括'00'大小写,以便用户知道期望的内容。 To allow for the optional +4 to be entered, update your regex to: 要允许输入可选的+4,请将您的正则表达式更新为:

var numbers = ^\d{5}(-\d{4})?$;  
if(zip.value.match(numbers))  
{
    ....
}
else
{
    ...
}

And finally the ultimate way to ensure that you are catching the correct zip code and that they are correctly associated with an address is to validate the whole thing (regardless of the length of the zip). 最后,确保您捕获正确的邮政编码并正确地将其与地址相关联的最终方法是验证整个内容(与邮政编码的长度无关)。 These validation services which you either make and maintain yourself or use a third party (such my company - http://www.qas.com/products/on-demand-address-validation.htm ) can speed the capture of an address and ensure it is valid - coping with 3, 4 or 5 digit zips, enhancing the address with missing +4, correcting typos in the zip or other address elements. 您自己制作和维护或使用第三方的这些验证服务(例如,我的公司-http: //www.qas.com/products/on-demand-address-validation.htm )可以加快地址的获取,并确保它是有效的-应对3、4或5位数的邮政编码,使用+4缺失来增强地址,更正邮政编码或其他地址元素中的拼写错误。

Hope that helps and good luck! 希望对您有帮助,祝您好运!

Al.

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

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