0

Can anyone help me here i am trying to validate information on 2 textboxs using JavaScript and i dnt know where am making mistak

<html> <body> <form method="post" onsubmit="return valid()"> name:<input type ="text" id="name1"> </br> Surname:<input type ="text" id="name2"> </br> <input type="submit" value="submit button"> </form> <script> function valid(){ var n1 = document.getElementById("name1"); var n2 = document .getElementById("name2"); if (n1.value !=){ if (n2.value !=){ alert("thank you for taking your time") return true } } alert("The values should be equal and not blank") return false } </script>

12th Nov 2017, 7:49 PM
Khorommbi Nkhangweleni
Khorommbi Nkhangweleni - avatar
5 Answers
- 1
Remove the ; before the { on your if statements
13th Nov 2017, 7:05 PM
John Wells
John Wells - avatar
+ 1
thanks
13th Nov 2017, 6:24 PM
Khorommbi Nkhangweleni
Khorommbi Nkhangweleni - avatar
+ 1
thanks its working now I did it like did if n1.value ="" && n2.value=""{ }
14th Nov 2017, 8:10 PM
Khorommbi Nkhangweleni
Khorommbi Nkhangweleni - avatar
0
my coding now is like this am having a problem if value its not null I got alert saying thanks for taking ur time also wen it is null am still getting the same alert saying thanks for taking ur time <html> <body> <form method="post" onsubmit="return valid()"> name:<input type ="text" id="name1"> </br> Surname:<input type ="text" id="name2"> </br> <input type="submit" value="submit button"> </form> <script> function valid(){ var n1 = document.getElementById("name1"); var n2 = document .getElementById("name2"); if (n1.value !=" ");{ if (n2.value !=" ");{ alert("thanks you for taking your time") ; return true; } } alert("The values should be equal and not blank") ; return false; } </script> </body> </html>
13th Nov 2017, 6:58 PM
Khorommbi Nkhangweleni
Khorommbi Nkhangweleni - avatar
- 2
You need something after != to compare with, "" should work. You need ; on a bunch of your statements.
12th Nov 2017, 8:12 PM
John Wells
John Wells - avatar