Results 1 to 8 of 8

Thread: JavaScript : Match error

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    JavaScript : Match error

    What is the error in this code:
    _______________________________

    var X="Hi [ ARE HOW";
    if(X.match('[')==('['))
    {
    alert("please Do not use [ ");
    return;
    }

  2. #2
    Hyperactive Member bsw2112's Avatar
    Join Date
    Nov 2001
    Location
    ottawa, canada
    Posts
    292
    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

  3. #3
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    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

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    [ 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.

  5. #5
    Fanatic Member punkpie_uk's Avatar
    Join Date
    Sep 2001
    Location
    UK
    Posts
    645
    match is used to check regular expressions against a string

    http://www.devguru.com/Technologies/...ing_match.html
    SPREAD THE WORD!!! Are You Lee McCormick? Because I Am



    Lee M McCormick
    [email protected]

    Lee McCormick.com - Live
    Dynamically Webbed.com - In development but live

  6. #6
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    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?

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    259

    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;
    }
    ______________________

  8. #8
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    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
  •  



Click Here to Expand Forum to Full Width