What's the best way in Javascript to validate that a field is alphanumeric?
A cynic knows the price of everything but the value of nothing.
Code: <script type="text/javascript"> //now tested: var str = "skslkdrt492" regexp1 = /\d/ regexp2 = /\w/ bool = true for (i=0; i<str.length; i++) { if (str.charAt(i).search(regexp1) == -1 && str.charAt(i).search(regexp2) == -1) { bool = false } } if (bool == false) { //not alpha-numeric. } </script>
<script type="text/javascript"> //now tested: var str = "skslkdrt492" regexp1 = /\d/ regexp2 = /\w/ bool = true for (i=0; i<str.length; i++) { if (str.charAt(i).search(regexp1) == -1 && str.charAt(i).search(regexp2) == -1) { bool = false } } if (bool == false) { //not alpha-numeric. } </script>
Last edited by Acidic; May 5th, 2004 at 12:09 PM.
Have I helped you? Please Rate my posts.
Forum Rules