Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    alphanumeric check

    I want to check whether an input field is alphanumeric. If not the form should not be submitted. Below is the code I am using in my function..

    Code:
    var valid="123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
    var sizechar = document.form.field.value.length;
    
    for (var i=0; i<sizechar; i++) {
         if (valid.indexOf(document.form.field.value.charAt(i)) < 0) {
         alert('Your password contains invalid characters.');
         document.form.field.value.focus();
         return false;
         }
    The problem:
    If the password is not alphanumeric I get the alert as expected. However, the form still submits. What am I doing wrong?

    Thanks!

  • #2
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I suspect you used something like this:
    Code:
    <form [...] onsubmit="validate(this)">
    you need to use something like this:
    Code:
    <form [...] onsubmit="return validate(this)">
    I suggest you learn how to use regular expressions.

    I also suggest you read these:
    The Behavior Layer
    Separating behavior and structure
    Behavioral Separation
    Unobtrusive JavaScript
    DOM Scripting - Sample chapter: Best Practices
    Accessible DHTML
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •