html - Javascript wrong redirecting when submitting -
My JavaScript code to see if the user entered the value for the user name and password field. When I run it, it shows me the right warning message, but the form is being presented, it also gives false returns. Can somebody help me out?
& lt; Script type = "text / javascript" & gt; Function loginValidate () {if (document.getElementById ("user name") value == '') {window.alert ("Please enter your username"); If (document.getElementById ("password"). Value == '') {window.alert ("Please enter your username and password"); return false; }} If (document.getElementById ("password"). Value == '') {window.alert ("Please enter your password"); return false; } Back true; } & Lt; / Script & gt; & Lt; Form onsubmit = "loginValidate ()" & gt; & Lt; Input type = "submit" name = "submit" id = "submit" value = "login" /> & lt; You must return the value given to the handler by the form / form P>
& lt; Form onsubmit = "returning validity validity ()" & gt; Also, do not give a name or an ID of "submit" any form, because it specifies the reference of the property's submit property By which the submit method of the mask form (i.e. form.submit refers to the control, so the form.submit () fails will go). There is rarely a need for the Submit button to place a name or ID, therefore:
You can also clear the argument a little bit. If you pass the function in the context of the forest, getting control is easier:
& lt; Script & gt; Function LoginValidate (form) {if (form.username.value == '') {window.alert ("Please enter your username"); return false; } If (form.password.value == '') {window.alert ("Please enter your password"); return false; } Back true; } & Lt; / Script & gt; & Lt; Form onsubmit = "Verify return login (this)" & gt; & Lt; Table & gt; & Lt; Tr & gt; & Lt; Td> Username: & lt; Td> & Lt; Input type = "article" name = "user name" & gt; & Lt; Tr & gt; & Lt; Td> Password: & lt; Td> & Lt; Input type = "password" name = "password" & gt; & Lt; Tr & gt; & Lt; Td colspan = "2" style = "text-align: right;> input type =" submit "value =" login "> <>> <>> Note that this sends passwords in clear text, not particularly secure.
Comments
Post a Comment