The main thing is where I stole it - it works and I don’t = (I can't do it. Tell me how to make it work.

$(document).ready(function() { function valid(form){ var error = false; var name = form.email.value; var name = form.phone.value; if(email == "" || email == " "){ error = "Вы не ввели email"; } else if (phone == ""){ error = "Вы не ввели телефон"; } if(error){ alert(error); } } }); 
 form{ padding: 20px; background-color: #fff; max-width: 320px; margin: auto; position: relative; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="form" method="post" name=""> <input type="text" name="email" placeholder="Введите email" i /> <input type="text" name="phone" placeholder="Введите телефон" /> <br /> <input type="button" onclick="valid(document.getElementById('form'))" name="submit" value="Готово" /> </form> 

  • one
    It would be better to help than to smack - kar man
  • For your basic check, js is not needed. It is enough to set the required attribute in the input. - Visman
  • 2
    Somewhere you stole code, you don’t know how to program or you don’t want to, but do you want it to be redone so that it works? Refer to the freelance exchange. - Nick Volynkin
  • one
    @karman helping here those who do and fails, and not to those who do not understand and just copy-paste the code without understanding - Alexey Shimansky

2 answers 2

  1. JQuery is not needed here.
  2. Here are the errors in these lines:

var name = form.email.value; //должно быть email=

var name = form.phone.value; //должно быть phone=

Here is the working code:

 <script> function valid(form){ var error = false; var email = form.email.value; var phone = form.phone.value; if(email == "" || email == " "){ error = "Вы не ввели email"; } else if (phone == ""){ error = "Вы не ввели телефон"; } if(error){ alert(error); } } </script> <form id="form" method="post" name=""> <input type="text" name="email" placeholder="Введите email" i /> <input type="text" name="phone" placeholder="Введите телефон" /> <br /> <input type="button" onclick="valid(document.getElementById('form'))" name="submit" value="Готово" /> </form> 

  • In principle, @Nagibaka all right said. - Nikolai Gordeev
  • Do not give answers to such questions. This way you provoke a person to further search for freebies. - Nick Volynkin

Remove the first and last line in your js code. Or move the function from $(document).ready(function() {}) You have a problem with the scope - your handler for clicking on the submission button does not see the validation function.

Added jQuery solution https://jsfiddle.net/g1qh8zom/4/

 // Validation function function valid(form) { var isValidMail = form.find('[name=email]').val().length <= 1 ? alert("Вы не ввели email") : true; var isValidName = form.find('[name=phone]').val().length <= 1 ? alert("Вы не ввели телефон") : true; return isValidMail && isValidName ? true : false; } // On load $(document).ready(function() { $('#form').on('submit', function() { return valid($(this)); }); }); 
 form { padding: 20px; background-color: #fff; max-width: 320px; margin: auto; position: relative; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="form" method="post" name=""> <input type="text" name="email" placeholder="Введите email" value="" /> <input type="text" name="phone" placeholder="Введите телефон" value="" /> <br /> <input type="submit" name="submit" value="Готово" /> </form> 

  • I made it, but it did not help - kar man
  • Do not give answers to such questions. This way you provoke a person to further search for freebies. - Nick Volynkin