Answer the question
In order to leave comments, you need to log in
How to check a text field for a specific phrase?
Please advise in this situation!
There is a text field where you need to enter the answer to the question, and a submit button!
How to use HTML and JavaScript to make it so that when you click on the submit button, the next page opens only with the correct answer, and if the answer is wrong, it displays a message about the wrong answer?
Answer the question
In order to leave comments, you need to log in
In the form, do input[type="text"] and button[type="submit"]. You hang a submit event handler on the form , take the value from the input and check it for correctness.
If it is correct, return true from the handler, if not - event.preventDefault() (an example of what you want at this link), show an error and return false.
If the validation is asynchronous (an AJAX request to the server), then everything will be more complicated, but the basic principle is the same - we intercept submit and check.
Don't even think about doing this on the client! Because I can open the code of the page and peek at the correct answer!
In my opinion, it is not possible to implement this only using HTML and JavaScript.
I'm not a JavaScript expert, but... how do you hide the response from the user so it can be seen in the page's code?
In general, the ideal option is an ajax request to the server. Well, if you really don’t want to process it using the server and don’t care that the user can see the source code of the page, then do something like this
<input type="hidden" name="answer" id="answer" value="Правильный ответ">
Well, of course, the js handler, when receiving data from the user, will compare it with the one in hidden and make a decision.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question