سؤال

To counter spam form messages, I want to have a contact form textarea that requires the user to include certain text in the message textarea and validate before submitting. A client-side script would check that text any placed inside the texatarea contained 'certain text'. The idea would be that you could require the user to add a pre-set word or copy and paste a line of text into the textarea like "This is a Valid Message". A more complex version might require the text match that from another input like a name or email address.

The closest I have found to validating (client-side) a required include is http://jqueryvalidation.org/url-method/

My theory is that this required text in a textarea would help reduce spam submissions by those that simply paste in random text and URLs.

Thanks for considering this question.

هل كانت مفيدة؟

المحلول

You can use JavaScript's indexOf() function:

var textToTest = document.getElementById("inputField").value;
var textToMatch = 'You need to type this here string!';

if(textToTest.indexOf(textToMatch) > -1) {
    //Legitimate user
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top