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

Validating the postcode so it can only start with LS

本文关键字:LS 开头 邮政编码 验证      更新时间:2023-09-26

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