Validaciones en Javascript
Validaciones en Javascript
On a user registration form, there are certain rules you want your users to follow when
inputting values to register on your site. Some of those rules include:
There are many more rules but let's leave it at these basic rules. In this tutorial, we are
going to perform form validation on all the input fields concerning the rules we just
listed.
register.php
index.php
scripts.js
style.css
<meta charset="UTF-8">
<div id="wrapper">
<div id="username_div">
<label>Username</label> <br>
<div id="name_error"></div>
</div>
<div id="email_div">
<label>Email</label> <br>
<div id="email_error"></div>
</div>
<div id="password_div">
<label>Password</label> <br>
</div>
<div id="pass_confirm_div">
<div id="password_error"></div>
</div>
<div>
</div>
</form>
</div></body></html><script type="scripts.js"></script>
#wrapper {
width: 30%;
padding: 50px;
background: #D7FBFF;}form {
height: 28px;
margin: 2px;
font-size: 1.2em;
padding: 5px;
width: 95%;}.textInput:focus {
outline: none;}.btn {
width: 98.6%;
border: none;
margin-top: 5px;
color: white;
background-color: #3b5998;
border-radius: 5px;
padding: 12px;}
Now, this is the file in which the real validation is done, the scripts.css:
username.focus();
return false;
document.getElementById('username_div').style.color = "red";
username.focus();
return false;
document.getElementById('email_div').style.color = "red";
email.focus();
return false;
document.getElementById('password_div').style.color = "red";
password.focus();
return false;
}
// check if the two passwords match if (password.value !=
password_confirm.value) {
document.getElementById('pass_confirm_div').style.color = "red";
return false;
if (username.value != "") {
document.getElementById('username_div').style.color = "#5e6e66";
name_error.innerHTML = "";
return true;
}}function emailVerify() {
if (email.value != "") {
document.getElementById('email_div').style.color = "#5e6e66";
email_error.innerHTML = "";
return true;
}}function passwordVerify() {
if (password.value != "") {
document.getElementById('pass_confirm_div').style.color =
"#5e6e66";
document.getElementById('password_div').style.color =
"#5e6e66";
password_error.innerHTML = "";
return true;
}
if (password.value === password_confirm.value) {
document.getElementById('pass_confirm_div').style.color =
"#5e6e66";
password_error.innerHTML = "";
return true;
}}
And that's all about the code. Now to run this, copy the code into a folder inside
htdocs or www folder of your PHP installation and log unto you browser and run it.
Once the page is displayed, try to enter some values and play around with the
validation.
I hope you find this helpful. Remember you can always support us by sharing.