D
D
dim5on2017-09-01 01:14:19
Validation
dim5on, 2017-09-01 01:14:19

How to make a simple JS validation inside an English test?

Hello comrades !
There is such a piece of a test for a site in JS in English:
Tell me how to do a simple validation, first to check that all questions have the input[type=radio] answer option, then the feedback form data (name, email, phone) ?

<form name="quiz" id="english-quiz"  method="post">
            <div class="container">
                <ol>
                    <script>
                        for(var q=0; q<questions.length; ++q) {
                            var question = questions[q];
                            var idx = 1 + q;
                            document.writeln('<li class="test-li"><span class="quest quote__main">' + question.text + '</span><br/>');
                            for(var i in question.answers) {
                            document.writeln('<input type=radio name="q' + idx + '" value="' + i + '" onClick="Engine(' + q + ', this.value)"  id="test-radio">' + question.answers[i] + '<br/>');
                        }
                    }
                    </script>
                </ol>
            </div>
            <div class="result-field">
                    <input type="text" placeholder="Имя" id="test-name">
                    <input type="tel" placeholder="Телефон" id="test-phone">
                    <input type="email" placeholder="Почта" id="test-email">
                    <input  class="btn btn-yellow btn-yellow--white footer__btn test-btn" placeholder="Получить результат" onClick="Score()">
                    <br>
                    <textarea name="area" id="textArea" cols="60" rows="30"></textarea>
            </div>
        </form>

var questions=[
{
    text: "Malaga ____ 754 years old.",
    answers: ["is", "are", "be"],
    correctAnswer: 0 // нумерация ответов с нуля!
},
{
    text: "She____believe it.",
    answers: ["dont", "isnt", "doesnt"],
    correctAnswer: 2
},
{
    text: "___there a river near Cathedral?",
    answers: ["are", "do", "is"],
    correctAnswer: 2
}
];

var yourAns = new Array;
var score = 0;

function Engine(question, answer) {yourAns[question]=answer;}

function Score(){
   var answerText = "Здравствуйте\n";
   for(var i = 0; i < yourAns.length; ++i){
    var num = i+1;
    answerText=answerText+"\n    Вопрос №"+ num +"";
    if(yourAns[i]!=questions[i].correctAnswer){
        answerText=answerText+" Не Верно!";
      }
        else{
        answerText=answerText+": Верно! \n";
        ++score;
        }
       }
    answerText=answerText+"\n Всего правильных ответов: "+score+ " из 25";


   if(score >= 0 && score <= 2){
    answerText=answerText+"\n Ваш уровень Beginner \n";
   }
   else if (score >= 3 && score <=11) {
    answerText=answerText+"\n Ваш уровень Elementary A1  \n";
   }      
   answerText=answerText+ "\n Имя пользователя: " + document.getElementById("test-name").value;
   answerText=answerText+ "\n Телефон пользователя: " + document.getElementById("test-phone").value;
   answerText=answerText+ "\n Почта пользователя: " + document.getElementById("test-email").value;
   document.getElementById("textArea").innerHTML = answerText;
   yourAns = [];
   score = 0;
   clearForm("quiz");
}
function clearForm(name) {
   var f = document.forms[name];
   for(var i = 0; i < f.elements.length; ++i) {
    if(f.elements[i].checked)
        f.elements[i].checked = false;
  }
    document.quiz.reset();
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Kornilov, 2017-09-01
@KorniloFF

set the required fields to the required attribute

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question