function jsre(theField) { var chk_pos...">

Javascript runtime error:

I urge the following JS to check the UK zip code:

<script type="text/javascript"> function jsre(theField) { var chk_postalcode = "^[A-Z0-9 ]*[AZ][A-Z0-9 ]*\d[A-Z0-9 ]*$|^[A-Z0-9 ]*\d[A-Z0-9 ]*[AZ][A-Z0-9 ]*$"; var txtpostalcode = document.getElementById("txtPostCode"); if (!chk_postalcode.test(txtpostalcode.value)) { alert("Valid"); } else { alert("Invalid"); } } </script> <asp:TextBox ID="txtPostCode" runat="server" onchange="jsre(this);"></asp:TextBox> 

I get a runtime error like:

 Error: Object doesn't support property or method 'test' 

I asked for help http://www.9lessons.info/2009/03/perfect-javascript-form-validation.html in the frame of my code.

Can someone help me how can I make the code work?

+4
source share
4 answers

"^[A-Z0-9 ][AZ][A-Z0-9 ]\d[A-Z0-9 ]$|^[A-Z0-9 ]\d[A-Z0-9 ][AZ][A-Z0-9 ]$" change it to /^[A-Z0-9 ][AZ][A-Z0-9 ]\d[A-Z0-9 ]$|^[A-Z0-9 ]\d[A-Z0-9 ][AZ][A-Z0-9 ]$/ . Remove the double quotation marks and place the forward slash.

+3
source

chk_postalcode is still a string , so it does not have a test() method.

turn it into a RegExp object:

 var chk_postalcode = /^[A-Z0-9 ]*[AZ][A-Z0-9 ]*\d[A-Z0-9 ]*$|^[A-Z0-9 ]*\d[A-Z0-9 ]*[AZ][A-Z0-9 ]*$/; 
+4
source

Must be:

 //add slashes and remove quotes var chk_postalcode = /^[A-Z0-9 ]*[AZ][A-Z0-9 ]*\d[A-Z0-9 ]*$|^[A-Z0-9 ]*\d[A-Z0-9 ]*[AZ][A-Z0-9 ]*$/; 
+3
source

try

 var chk_postalcode = /^[A-Z0-9 ]*[AZ][A-Z0-9 ]*\d[A-Z0-9 ]*$|^[A-Z0-9 ]*\d[A-Z0-9 ]*[AZ][A-Z0-9 ]*$/; 
+2
source

Source: https://habr.com/ru/post/1402178/


All Articles