Javascript: how to validate if text is meaningful in an input field | Sololearn: Learn to code for FREE!

+1

Javascript: how to validate if text is meaningful in an input field

I would like to validate if a subscriber typed in meaningful English text in a form instead of random characters as e.g. 'jaghdjagqfsg'. Are there use-case recipes of filtering out nonsense text? E.g. for onsubmit event: function validateForm() { var x = document.forms["myForm"]["fname"].value; // I need rules here: if (x == "jaghdjagqfsg") { alert("Please answer the question"); return false; } }

8/5/2020 7:39:46 PM

Prof. Dr. Zoltán Vass

6 Answers

New Answer

+3

There are systems and AI to check for valid grammar, but it's definitely something you shouldn't get concerned with unless you own a bigger team. The best idea I could suggest is to split every single word by spaces and remove quotes, commas, colons etc. and check if the word in there is valid by checking it through the list of all valid English words. http://www.mieliestronk.com/corncob_lowercase.txt You can make a code to turn the text file to a javascript array and compare every single word in the paragraph.

+3

That is if the problem is for commercial purposes instead of learning purposes. If it is for learning, it is always suggested to make all of the core parts of your code by yourself, because it helps you dig deeper into topics you've not touched before.

+2

Prof. Dr. Zoltán Vass you could theoretically create a regular expression that you test the textarea input against, using an english dictionary as suggested by Coder’s Crux. You might probably want to allow inputs even if for instance only 80% of the words are detected as “correct english words”, to avoid ommiting inputs due to a typo or use of a non-dictionary word. A good description an basic form validation example is given in the later part of the text here: https://developer.mozilla.org/en-US/docs/Learn/Forms/Form_validation

0

Coder's Crux Thanks for the advice, the problem is better defined now.

0

This problem is not solved by js or some other language directly. I think you need to search some 3part library for solving such problem

0

phil Cool! I especially like the 80% idea :) Also thanks for the link