【问题标题】:Validating the postcode so it can only start with LS验证邮政编码,使其只能以 LS 开头
【发布时间】:2015-05-26 15:53:57
【问题描述】:

我希望能够验证文本框,以便它只接受以 LS 开头的邮政编码。

这是因为这家公司只在利兹地区运送包裹,有点噩梦。

<!DOCTYPE html>
<html>
<body>

 <form name="myForm" action="stage2.php" onsubmit="return validateForm()"   method="post">
 Postcode: <input type="text" name="pcode">
 <input type="submit" value="Submit">
 </form>

 </body>
 </html>

这是我正在使用的 Javascript

<script>
function validateForm() {
var x = document.forms["myForm"]["pcode"].value;
if (x == "LS" || x == "") {
    alert("Name must be filled out");
    return false;
    }
 }
</script>

【问题讨论】:

    标签: javascript validation postal-code


    【解决方案1】:
    <script>
        function validateForm() {
           var x = getElementsByName("pcode").value; 
           if (x == "LS" || x == "" || !(x.indexOf("LS")==0)) {
              alert("Name must be filled out");
              return false;
           }
        }
    </script>
    

    【讨论】:

    • 你好,不管你输入什么,它都没有工作它只是提出警报
    猜你喜欢
    • 1970-01-01
    • 2013-10-09
    • 2018-07-21
    • 2014-09-08
    • 1970-01-01
    • 2022-12-11
    • 2013-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多