View Full Version : Not allow special Chars

03-14-2012, 10:52 AM

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

expression: "if (VAL) return true; else return false;",

message: "Please enter your name"
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"

03-14-2012, 11:10 AM
I have found the following piece of code but am slightly unsure where to put it and how it would work

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;

03-14-2012, 11:39 PM
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):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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-z0-9]*$/i).test(f.value)?f.value = f.value.replace(/[^A-z0-9]/ig,''):null;
<form id="myform" action="">
<input name="mytext" type="text" onkeyup="valid(this)" onblur="valid(this)">

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.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum