...

View Full Version : (e) problem



Meticulous
08-20-2008, 04:50 AM
Hi,

Currently i'm using a function to check the user from entering special characters in the textbox as below

function IsAlphaNumber(e)
{
// Logic
}

In a testing machine if fails by allowing the special characters and show object expected errors. :confused:

Can any one suggest me any work around.

Thanx :)

Meticulous

m_sundar
08-20-2008, 05:06 AM
Hey Meticulous try this one, it will work fine, When u call this function in the onclick event pass the parameter like this "..... onclick="return ValidateField(event)" .... "
---------------------------------------------------
function ValidateField(e)
{
//debugger
var asciiVal;
var returnVal = false;

if(window.event) // If browser type is IE
{
asciiVal = e.keyCode;
}
else if(e.charCode) // If browser type is Netscape/Firefox/Opera
{
asciiVal= e.charCode;
}

// Key Code Values :
// 32 = " "
// 47 = "/"
// 48 - 57 = 0 -9

if (asciiVal > 31 && (asciiVal < 47 || asciiVal > 57))
alert("Only numbers and '/' are allowed");
else
returnVal = true;

return returnVal;
}
------------------------------------------------------------------

sundar :thumbsup:

Philip M
08-20-2008, 07:56 AM
Aa much better validation is to use a regex:-


<input type = "text" id = "txt1" size = "12" onblur = "validateField(this)">

<script type = "text/javascript">
function validateField(which) {
fldval = which.value;
fldval = fldval.replace(/^\s+/,""); // strip leading spaces
if (/[^a-z0-9\s]/i.test(fldval)) { // only alphanumeric and space allowed
alert ("You have entered one or more invalid characters - try again!");
which.value = "";
which.focus();
return false;
}
}
</script>
If spaces are not allowed delete the \s in blue.


Why should I do anything for posterity? What has posterity ever done for me? Groucho Marx (1890 - 1977)

m_sundar
08-20-2008, 08:08 AM
Hey Philip , I do agree to your post,

The script which i posted doesn't allow the user to enter the invalid character. But in ur case the user can enter the invalid character and then when he changes the focus he gets the alert msg. Also when u use the onBlur event the control loses focus after the alert box, so user has to again click on the control to edit it, this becomes annoying. So think before using onBlur event.

If u have a better solution as an alternative to the onBlur event plz let me know.

Thnx... :-)

sundar

Philip M
08-20-2008, 08:37 AM
Also when u use the onBlur event the control loses focus after the alert box, so user has to again click on the control to edit it, this becomes annoying.
sundar

No, that is incorrect, did you not notice:-

alert ("You have entered one or more invalid characters - try again!");
which.value = "";
which.focus();

The OP requested no special characters, not numbers only.

If you want a script which prevents special characters from being entered in the first place or pasted in then:-


<script type="text/JavaScript">
function valid(f) {
f.value = f.value.replace(/[^a-z0-9\s]/gi,""); // only alphanumeric and space allowed
}
</script>

<form id="myform" action="">
<input name="mytext" type="text" onkeyup="valid(this)" onblur="valid(this)">
</form>

As always server-side checking is also required as Javascript may be disabled.

It is your responsibility to die() if necessary….. - PHP Manual

m_sundar
08-20-2008, 08:50 AM
Hey Philip,

I have made a mistake , its not onclick , it is onkeypress event.. that was my mistake...oops...

And the key code i have given over there is just for an example, the OP has to modify according to his needs,

Also when u use onkeyup event u have to use which.value = "" , u cant avoid it bcos the invalid character will remain over there unless u clear it manually.

thnx and regards

Sundar :-)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum