I have a text field and would like to stop the user from entering "/" and "\" characters.
I dont want to validate this on submission of a form but would rather supress this as the users are typing.
Can anyone help ?
Printable View
I have a text field and would like to stop the user from entering "/" and "\" characters.
I dont want to validate this on submission of a form but would rather supress this as the users are typing.
Can anyone help ?
Something similar to this:
Code:<input type=text name=num2 onchange="checkvalue(this,200)">
http://www.scit.wlv.ac.uk/~jphb/java...ples/ex9d.htmlCode:<script>
<!--
function checkvalue(field,limit)
{
if(field.value > limit)
{
alert("Out of range in " + field.name);
field.value = 0;
}
}
-->
</script>
ober, ***??
this should do it:
Code:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<script language="JavaScript" type="text/javascript">
<!--
function check_for_evil_characters(str) {
regexp=/\\/
if (str.value.search("/") != -1)
{
output = str.value.substring(0,str.value.search("/"))
str.value = output
}
else if (str.value.search(regexp) != -1)
{
output = str.value.substring(0,str.value.search(regexp))
str.value = output
}
}
//-->
</script>
<input type="text" onkeyup="check_for_evil_characters(this)">
</body>
</html>
I wasn't telling him how to do it. I was giving him an example of a method. :rolleyes:
Acidic, ***? Sorry, couldn't help it.
Remove the space in JavaScript added by the forum.Code:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>JavaScript Test</title>
<script type="text/javascript">
function myFoo(myInput) {
myInput.value = String(myInput.value).replace(new RegExp("/\/|\\/", "g"), "");
}
</script>
</head>
<body>
<p><input type="text" onkeyup="javascript:myFoo(this);"></p>
</body>
</html>
Wow... that is pretty if I do say so myself. Too bad I'm not asked to do stuff like this for a job interview.
You still need to validate on the server side incase they disable JavaScript.
I bow my head. I didn't think of using replace(), or an OR in regexp. donnu why. Thanks for the knock over the head.
Thanks a lot.
I applaud your genius and pray that God will rainy pretty flowers on all of your heads.
:wave:
I have 2 wee problems with this.
How can I also check for sinle and double speach marks ?
How does javascript handle these and what is the "g" for ?
g stands for global, so it searches all the instances, not only the first. Not really necessary in this case.Quote:
Originally posted by venerable bede
How does javascript handle these and what is the "g" for ?
for speach marks you might have to search for the ASCII value of them, not quite sure what that is.
and sinle? Which character is that?
True, the global flag isn't necessary, just habit. The global would be necessary if you were triggering on onBlur.
As to the double and single quotes:
You may notice that this isn't looking for backslashes. Seems my old code wasn't finding them, either. I haven't figured out why. Just figured I'd post this until I fixed the problem.Code:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>JavaScript Test</title>
<script type="text/javascript">
function myFoo(myInput) {
myInput.value = String(myInput.value).replace(new RegExp("['\/\"]", "g"), "");
}
</script>
</head>
<body>
<p><input type="text" onkeyup="javascript:myFoo(this);"></p>
</body>
</html>
hmm. my old code did look for them. just add that IF satement to the ned of this script.