验证邮政编码,使其只能以LS开头

时间:2015-05-26 15:53:57

标签: javascript validation postal-code

我希望能够验证文本框,以便它只接受以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 I&#39; m

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

1 个答案:

答案 0 :(得分:0)

<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>