knightrgv
asked on
restrict the user to eneter numbers,alpahbets and @ in a input box...
Hi,
How can i restrict the user to eneter numbers,alpahbets and @ in a input box.
George
How can i restrict the user to eneter numbers,alpahbets and @ in a input box.
George
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
function checkVal(obj)
{
var validCharacters="abcdefghi jklmnopqrs tuvwxyz";
var curVal = obj.value;
var tmpVal = ""
var bValid = true;
for(i=0;i<curVal.length;i+ +)
if(validCharacters.indexOf (curVal.ch arAt(i))<0 )
tmpVal += curVal.charAt(i);
else bValid=false
if(bValid==false)
{
alert("illegal characters encountered");
obj.value=tmpVal;
}
}
and call it like
<input type=text onKeyUp="checkVal(this)">
Regards,
CJ
{
var validCharacters="abcdefghi
var curVal = obj.value;
var tmpVal = ""
var bValid = true;
for(i=0;i<curVal.length;i+
if(validCharacters.indexOf
tmpVal += curVal.charAt(i);
else bValid=false
if(bValid==false)
{
alert("illegal characters encountered");
obj.value=tmpVal;
}
}
and call it like
<input type=text onKeyUp="checkVal(this)">
Regards,
CJ
oh, and add any valid characters to the validCharacters variable.
That is exactly what that bit of code will do. It's called regular expressions. What that line means is that will allow characters (a to z) and numbers (0 to 9) as well as one @ symbol and at least one period (.)
Regards,
CyberSoft
Regards,
CyberSoft
ASKER
Hi,
Thank you for the code. But one more problem.
How come i am able to enter only one character in my input box? What should i change to enter more than one.
Knightrgv
Thank you for the code. But one more problem.
How come i am able to enter only one character in my input box? What should i change to enter more than one.
Knightrgv
knightrgv,
the original comment by CyberSoft gave you WILL work.
He is right, it is a regular expression which is the most efficient way to do any sort of [string] validation/manipulation.
You can also enter more than one character in his example.
Cut-n-paste it and test it - see for your self.
You should give him credit for that comment.
--labcoat
the original comment by CyberSoft gave you WILL work.
He is right, it is a regular expression which is the most efficient way to do any sort of [string] validation/manipulation.
You can also enter more than one character in his example.
Cut-n-paste it and test it - see for your self.
You should give him credit for that comment.
--labcoat
Thanks labcoat - an added advantage is it's also cross-browser compatible.
Regards,
CyberSoft
Regards,
CyberSoft
ASKER
The code worked great. Thank you.
Always a pleasure to help out. After all thats why we're here.
Regards,
CyberSoft
Regards,
CyberSoft
ASKER
I am sorry. But i am not able to interpret this
var emailexp = /^[a-z][a-z_0-9\.]+@[a-z_0
I only want the user to restricy the user from entering anything other than
1.numbers
2.alphabets
3.@
Can you help some more?
Thanks again,
Knightrgv