The submit button should popup an alert message saying that the pattern on postcode and zipcode are wrong when user input it wrong. But I can't seem to make it work. I don't want to use the "pattern=" in form. I want to display an alert box. And I need to use the RegExp method. Any ideas why?
This is my code:
<script>
function validation(){
var icn = document.getElementById("icno").value;
var postcode = document.getElementById("pstcode").value;
var regexp = new RegExp(icn,"\d{6}-\d{2}-\d{4}");
var regexp1 = new RegExp(postcode,"[0-9]{5}");
if (regexp.exec(icn)){
return true;
}
else{
alert("Please enter your IC correctly")
}
if (regexp1.exec(postcode)){
return true;
}
else{
alert("Please enter your postcode correctly")
}
}
</script>
</body>
</html>
var regexp = /\d{6}-\d{2}-\d{4}/; var regexp1 = /[0-9]{5}/;- and add^/$anchors if you need a full string match.