www.webdeveloper.com
Results 1 to 4 of 4

Thread: [newbie] Anti-SPAM + form validator?

Hybrid View

  1. #1
    Join Date
    Jun 2009
    Posts
    28

    [newbie] Anti-SPAM + form validator?

    Hello

    This must be a very simple problem for JS experts, but I don't know enough yet about JS to know how to solve it.

    After successfully adding and using jQuery's form validator, I'd like to add a simple "Please computer a+b" anti-SPAM check.

    The problem is how to combine the two?

    Here's a simplified example:
    Code:
    <head>
    	<script src="http://code.jquery.com/jquery.min.js"></script>
    	<script type="text/javascript" src="js/jquery.validate.js"></script>
    	
    	<script type="text/javascript">
    		//========= jQuery form validator
    		jQuery.extend(jQuery.validator.messages, {
    		    required: "This field is required.",
    		    email: "Please enter a valid e-mail address."
    		});
    
    		//========= Simple, anti-spam scheme  
    		var a = Math.ceil(Math.random() * 10);
    		var b = Math.ceil(Math.random() * 10);       
    		var c = a + b
    		
    		function DrawBotBoot()
    		{
    		    document.write("Compute "+ a + " + " + b +"? ");
    		    document.write("<input id='BotBootInput' type='text' maxlength='2' size='2'/>");
    		}    
    		function ValidBotBoot(){
    		    var d = document.getElementById('BotBootInput').value;
    		    if (d == c) return true;
    		    return false;
    		}
    		
    		//========= ?How to check anti-spam answer + validate other fields in form?
    		$(document).ready(function(){
    		  $("#myform").validate({
    		  	/*submitHandler: function(form) {
    		       form.submit();
    				}
    			});*/
    		};
      </script>
    </head>
    
    <body>
    
    <form action="send.php" method="post" id="myform">
    	Name <input type="text" maxlength="25" id="name" name="name" class="required"/>
    	Anti-SPAM check<script type="text/javascript">DrawBotBoot()</script>
    
    	<input type="submit" class="submit" value="Send" onclick="alert(ValidBotBoot());"/>
    </form>
    
    </body>
    Thanks for any help.

  2. #2
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,264
    1/ - Do not use document.write in a function which run after the document is loaded !

    2/ -
    Code:
    	// this two lines
    	if (d == c) return true;
    	return false;
    	// can be replaced by
    	return d==c;
    3/- Add an if(ValidBotBoot()) before the form.submit();
    Last edited by 007Julien; 11-29-2012 at 10:38 AM. Reason: complements

  3. #3
    Join Date
    Jun 2009
    Posts
    28
    Thanks for the help. I don't understand how to...
    1. Remove the DrawBotBoot() function since it's in charge of displaying the question asked to the user
    2. How to have jQuery validate the form, including the anti-SPAM question.

    Does someone know of a simple anti-SPAM solution provided by jQuery that I could easily add to its form validator?

  4. #4
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,264
    Sorry for 1/- I have nor seen the script in HTML !

    3/- jQuery is not my cup of tea ! The second example of this jQuery page should solve the question

    Adds a validation method that checks if a given value equals the addition of the two parameters.
    Code:
     
    jQuery.validator.addMethod("math", function(value, element, params) { 
     return this.optional(element) || value == params[0] + params[1]; 
    }, jQuery.format("Please enter the correct value for {0} + {1}"));
    Last edited by 007Julien; 11-29-2012 at 05:31 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles