SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2006
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question check input string whether matching to certain keywords...[WHERE IS WRONG?]

    may i know how to check the input string of a user when he clicks on submit button?

    i wanna detect the string and if found words like "SX" , "XXX", "xxx", it will return a message where they are not allowed to post the message which contains the above words.

    thank you!
    Last edited by liyenn; Nov 23, 2006 at 20:13. Reason: using with database data

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The JavaScript match() Method will handle that. However, this really needs to be in the server-side processing instead or in addition to any JavaScript code you write, as any client side coding you do can be disabled or bypassed.

  3. #3
    SitePoint Zealot
    Join Date
    Nov 2006
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Quote Originally Posted by Dan Grossman
    The JavaScript match() Method will handle that. However, this really needs to be in the server-side processing instead or in addition to any JavaScript code you write, as any client side coding you do can be disabled or bypassed.
    Code:
    <script language="javascript>
    
    function check()
    {
    	valid= true;
    	var str="xxx";
    	if ( document.add_post.msg.value == str)
    		{
    		alert ("Sorry, your post is not allowed!");
    		valid = false;
    		}
    		return valid;
    }
    </script>
    
    		<form name="add_post">
    		<input type="text" name="msg">
    		<input type="submit" value="submit" onclick="check()">
    		</form>
    hi, i have tried on this. it will return the alert "Sorry, your post is not allowed!" when the user enter "xxx"in the msg textfield.

    however, can i make it if the user type in "welcome to xxx page" in msg, it will detect it and showing the alert ?

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by liyenn
    may i know how to check the input string of a user when he clicks on submit button?

    i wanna detect the string and if found words like "SX" , "XXX", "xxx", it will return a message where they are not allowed to post the message which contains the above words.

    thank you!
    It's pretty straightforward (but seems hardly useful)

    Code:
    if(document.add_post.msg.value.match(/SX|XXX/i))
       valid = false;

  5. #5
    SitePoint Zealot
    Join Date
    Nov 2006
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog
    It's pretty straightforward (but seems hardly useful)

    Code:
    if(document.add_post.msg.value.match(/SX|XXX/i))
       valid = false;

    thanks for your suggestion~
    I'll try on this...

    mmm~is this kind of regular expression?
    (sorry,i'm newbie~ wish to learn more from you especially on regular expression...)

  6. #6
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, this is a regular expression.

    google for "regular expression tutorial", there are tons of links.

  7. #7
    SitePoint Zealot
    Join Date
    Nov 2006
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog
    yes, this is a regular expression.

    google for "regular expression tutorial", there are tons of links.

    =) i have been surfing on regular expression the whole day...
    found they are more on description (the usage)...
    hardly find example on that..
    too complicated for me... hehe...

    however, i got what i want after trying on your suggestion..

    thank you!

  8. #8
    SitePoint Zealot
    Join Date
    Nov 2006
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how if i wish to get the banned text from database?

    how if i wish to get the banned text from database?
    i have tried on the above code, but error occur...

    may i know where is wrong?

    Code:
    function validate_form ()
    {
    	valid = true;
    	<?php 
    	$query_banned = "SELECT * FROM banned";
    	$result_banned = mysql_query($query_banned);
    	while ($row=mysql_fetch_assoc($result_banned))
    	{									
    	$tmpWord=$row["word"];  
    	?>
    	
    	if(document.add_post.msg.value.match(/<?php echo $tmpWord."|" ?>/i));
    	alert ("Sorry, your post is not allowed!");
    	valid = false;
    	<?php 
    	} 
    	?>
    	}

  9. #9
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,789
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Code:
    if(document.add_post.msg.value.match(/<?php echo $tmpWord."|" ?>/i));
    	alert ("Sorry, your post is not allowed!");
    	valid = false;

    should be

    Code:
    if(document.add_post.msg.value.match(/<?php echo $tmpWord."|" ?>/i)) {
    	alert ("Sorry, your post is not allowed!");
    	valid = false;
    }
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  10. #10
    SitePoint Zealot
    Join Date
    Nov 2006
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh~~

    hehe, ok I'll fix it~ thank you!!! hehehe~

  11. #11
    SitePoint Zealot
    Join Date
    Nov 2006
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi, i'm doing some modification like below,. everything works fine except it do not is not case-insensitive although i set it to '/i'. Is there anything i missed?


    PHP Code:
    <SCRIPT LANGUAGE="javascript">
    function validate_form()
    {
        if (document.add_post.adtitle.value.match(document.add_post.word.value))
            {
            alert ("NOTE: Your post is awaiting for approval!");
            document.add_post.verified.value="0";
            }    
    </script>


    <?php

    //BANNED WORD
    $query_banned "SELECT * FROM banned";
    $result_banned mysql_query($query_banned);
    while (
    $row=mysql_fetch_array($result_banned))
        {                                    
        
    $word=empty($word) ? $row['word'] : $word "|" $row['word'];  
        }
        
        
    $word="/|".$word."|/i";
        echo 
    "<input type='hidden' name='word' value='".$word."'>";
        
        
    ?>

  12. #12
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please read the docs on regular expressions in javascript

    http://developer.mozilla.org/en/docs...Objects:RegExp


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •