|
-
Aug 10th, 2002, 06:24 AM
#1
Thread Starter
Hyperactive Member
JavaScript : Match error
What is the error in this code:
_______________________________
var X="Hi [ ARE HOW";
if(X.match('[')==('['))
{
alert("please Do not use [ ");
return;
}
-
Aug 13th, 2002, 11:20 AM
#2
Hyperactive Member
hello,
i don't know what match is but this is what I would do
specify a list of bad chars and then just test your string against each one (tested in IE6 only)
i put it in a textbox
Code:
<html>
<head>
<script>
var badChars = new Array("@", "#", "[", "]");
function validateString(frm1) {
var valString = frm1.boo.value;
for (ndx = 0; ndx<badChars.length;ndx++)
{
for (ndx2=0;ndx2< valString.length; ndx2++)
{
if (valString.charAt(ndx2) == badChars[ndx]) {
alert("error");
frm1.boo.focus;
return false; }
}
}
return true;
}
</script>
</head>
<body>
<form name = foo action = "blank.cgi" onSubmit= "return validateString(this)" method=POST>
<input type = "text" name = "boo"><br><br>
<input type = "submit">
</form>
</body>
</html>
bsw2112
-
Aug 13th, 2002, 01:22 PM
#3
Frenzied Member
I may be totally wrong here, but isn't match a RegExpr thing? If it is I don't think you are specifying X as a regular expression. But as I say, I could be way off
-
Aug 13th, 2002, 02:59 PM
#4
Thread Starter
Hyperactive Member
[ is the cause of problem
In this code:
____________
var X="Hi [ ARE HOW";
if(X.match('[')==('['))
{
alert("please Do not use [ ");
return;
}
___________
when I replace [ with any other letter or number the code work with out problem. so [ is the cause of this problem.
-
Aug 14th, 2002, 02:57 AM
#5
Fanatic Member
match is used to check regular expressions against a string
http://www.devguru.com/Technologies/...ing_match.html
-
Aug 14th, 2002, 05:16 AM
#6
Frenzied Member
So it is a RegExpr? [ and ] are used to group strings aren't they? So you should be able to escape them with \[ and \] shouldn't you?
-
Aug 14th, 2002, 06:37 AM
#7
Thread Starter
Hyperactive Member
Then,please
How can I right this code in right way?
______________________
var X="Hi [ ARE HOW";
if(X.match('[')==('['))
{
alert("please Do not use [ ");
return;
}
______________________
-
Aug 14th, 2002, 01:59 PM
#8
Frenzied Member
Not sure, what I said didn't work after all
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
|