Pregunta

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.

¿Fue útil?

Solución

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
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top