0

I have to validate a textbox having postal code.The postal code can be two formats as possible
xxxxxxxxx or
xxxxx-xxxx.

My question is can I use two javascript regex for validating a textbox?

2
Contributors
2
Replies
11
Views
3 Years
Discussion Span
Last Post by amrita111
0

You can use the javascript test method. This method can be used to test for a match in a string. It returns true (match) or false (no match).

<input type="text" id="zip" onkeyup="isValid()" /><br/>
<span id="result"></span>


<script>
function isValid(){
   var zip = document.getElementById('zip');
   var result = document.getElementById('result');
   var isValidZip = /(^\d{9}$)|(^\d{5}-\d{4}$)/.test(zip.value);
   if (isValidZip) { 
    result.innerHTML = "Valid";
   } else {
    result.innerHTML = "Not Valid";
  }
}
 </script>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.