|
-
Feb 27th, 2004, 11:57 AM
#1
Thread Starter
Fanatic Member
Javascript supress characters
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 ?
-
Feb 27th, 2004, 12:59 PM
#2
Frenzied Member
Something similar to this:
Code:
<input type=text name=num2 onchange="checkvalue(this,200)">
Code:
<script>
<!--
function checkvalue(field,limit)
{
if(field.value > limit)
{
alert("Out of range in " + field.name);
field.value = 0;
}
}
-->
</script>
http://www.scit.wlv.ac.uk/~jphb/java...ples/ex9d.html
-
Feb 27th, 2004, 02:44 PM
#3
Frenzied Member
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>
Have I helped you? Please Rate my posts. 
-
Feb 27th, 2004, 03:20 PM
#4
Frenzied Member
I wasn't telling him how to do it. I was giving him an example of a method.
-
Feb 27th, 2004, 03:46 PM
#5
Addicted Member
Acidic, ***? Sorry, couldn't help it.
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>
Remove the space in JavaScript added by the forum.
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.
Travis, Kung Foo Journeyman
Web Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.5 Guide and Reference
Perl: Documentation, Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
OSS: Mozilla, MySQL (Manual)
-
Feb 27th, 2004, 05:53 PM
#6
Frenzied Member
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.
Have I helped you? Please Rate my posts. 
-
Mar 1st, 2004, 04:28 AM
#7
Thread Starter
Fanatic Member
Thanks a lot.
I applaud your genius and pray that God will rainy pretty flowers on all of your heads.
-
Mar 1st, 2004, 04:31 AM
#8
Thread Starter
Fanatic Member
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 ?
-
Mar 1st, 2004, 08:01 AM
#9
Frenzied Member
Originally posted by venerable bede
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.
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?
Have I helped you? Please Rate my posts. 
-
Mar 1st, 2004, 08:36 AM
#10
Addicted Member
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:
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>
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.
Travis, Kung Foo Journeyman
Web Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.5 Guide and Reference
Perl: Documentation, Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
OSS: Mozilla, MySQL (Manual)
-
Mar 1st, 2004, 09:08 AM
#11
Frenzied Member
hmm. my old code did look for them. just add that IF satement to the ned of this script.
Have I helped you? Please Rate my posts. 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|