Validating form with javascript for php

I find this code in W3Schools. This is about validating a form using Javascript.

When the user clicks the wrong mail format, a warning appears.

When I check the code that I see in the form onsubmit="return validateForm(); the function returns false in case of an error.

So my question is how to get a boolean from javascript so that it can be used in PHP to write an error message instead of using a message box.

This is my code:

 <html> <head> <script type="text/javascript"> function validateForm() { var x=document.forms["myForm"]["email"].value var atpos=x.indexOf("@"); var dotpos=x.lastIndexOf("."); if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) { alert("Not a valid e-mail address"); return false; } } </script> </head> <body> <form name="myForm" action="demo_form.asp" onsubmit="return validateForm();" method="post"> Email: <input type="text" name="email"> <input type="submit" value="Submit"> </form> </body> </html> 

Thanks in advance.

Ali

+8
javascript php validation forms
source share
5 answers

You cannot get boolean value from javascript. What you need to do is prepare a div error in advance. Something like that:

 <body> <form name="myForm" action="demo_form.asp" onsubmit="return validateForm();" method="post"> Email: <input type="text" name="email"> <div id="error"></div> <input type="submit" value="Submit"> </form> 

Add css to it to hide it. Then in your validation form, use javascript to change the HTML error code.

  if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) { document.getElementById("error").innerHTML="Not a valid e-mail address"; return false; } 

"return false"; the part must inform the form not to file. As mentioned in other answers, PHP and Javascript do not interact. PHP - server side, Javascript - client side. They do not talk to each other. However, they can modify the HTML. Just remember, Javascript always works in the browser. Therefore, if he needs to send things to PHP, you need to use AJAX to call the server side of the PHP script and send the information in this way. However, most of the time when you use Javascript, it is not PHP that needs information, it is a user. Therefore, using Javascript to change the HTML does the trick.

+7
source share
 if (..) { document.getElementByID('error').InnerHTML = "Not a valid e-mail address!"; return false; } else { document.getElementByID('error').InnerHTML = ""; } 

and in your markup add:

 <span id="error"></span> 
+6
source share

Outside of using AJAX you cannot. Remember that PHP is a server language, and JavaScript runs on the client in a browser. By the time PHP displays the page in the browser, it is processing. You need two levels of validation: JavaScript validation and PHP validation. What for? Because JavaScript can be disabled in the browser. This makes it useless as a security measure. JavaScript validation should only be used as a way to improve user experience.

Finally, do not go to w3schools. Most of his information is invalid (see the website http://www.w3fools.com for more information). Use the Mozilla Developer Network instead: https://developer.mozilla.org/en-US/learn/

+3
source share

The easiest way is to map to a function. Replace the alert with something like: -

document.getElementById ("errormessage"). innerHTML = "Invalid email address";

You will also need a DIV in your HTML with the errormessage identifier.

+3
source share

I would not use PHP for this.

 <html> <head> <script type="text/javascript"> function validateForm() { var x=document.forms["myForm"]["email"].value var atpos=x.indexOf("@"); var dotpos=x.lastIndexOf("."); if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) { document.getElementById('errorMsg').style.display = 'block'; return false; } } </script> </head> <body> <form name="myForm" action="demo_form.asp" onsubmit="return validateForm();" method="post"> Email: <input type="text" name="email"> <input type="submit" value="Submit"> <div id="errorMsg" style="display: none;">Not valid email</div> </form> </body> </html> 

This way you do not have to return to your server.

+3
source share

All Articles