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 3 of 3
  1. #1
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts

    Question Not allow special Chars

    hi,

    I would like to change my validation page so that a user cannot use special characters in the form, here is my code below

    Code:
    				jQuery("#full_name").validate({
                        expression: "if (VAL) return true; else return false;",
    
                        message: "Please enter your name"
                    });
    				jQuery("#name").validate({
                        expression: "if (VAL.match(/^[^\\W][a-zA-Z\\_\\-\\.]+([a-zA-Z\\_\\-\\.]+)*\\@[a-zA-Z0-9_]+(\\.[a-zA-Z0-9_]+)*\\.[a-zA-Z]{2,4}$/)) return true; else return false;",
    
                        message: "Please enter your name"
                    });

  • #2
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts
    I have found the following piece of code but am slightly unsure where to put it and how it would work


    Code:
     var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
            for (var i = 0; i < document.formname.fieldname.value.length; i++) {
                    if (iChars.indexOf(document.formname.fieldname.value.charAt(i)) != -1) {
                    alert ("The box has special characters. \nThese are not allowed.\n");
                    return false;
            }
                    }

  • #3
    Regular Coder Taro's Avatar
    Join Date
    Oct 2011
    Location
    Geraldton, Ontario
    Posts
    155
    Thanks
    1
    Thanked 1 Time in 1 Post
    Hello Jarv,

    First I would suggest that you can try putting the piece of coding before the rest of the script because you are assigning the variable.

    Other than that, you can try a different way of blocking the special character keys with the script in this html code (may have to edit it to meet your needs):

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <script type="text/JavaScript">
    function valid(f) {
    !(/^[A-zÑñ0-9]*$/i).test(f.value)?f.value = f.value.replace(/[^A-zÑñ0-9]/ig,''):null;
    } 
    </script>
    </head>
    <body><br>
    <form id="myform" action="">
    <input name="mytext" type="text" onkeyup="valid(this)" onblur="valid(this)">
    </form>
    </body>
    </html>
    Moreover, there is another alternative: you can use the DEC code from an ASCII table to convert the character keys. Once you have the numbers, you can return each key-press as false before it is even written on the form/ disable them.
    Element ID

    Webs Support Helper

    Your friendly neighborhood Taroman.


  •  

    Posting Permissions

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