Q:
Javascript form validation not working with form
I've been trying to get this validator function working for the past 3 days.
It's not giving me any errors, but my html input field is not being validated.
I've checked out other stack overflow posts but they don't work either.
I am using a WAMP stack.
Here's my code:
HTML:
CSS:
.form_content input {
width: auto;
height: 25px;
background-color: #e6f9ff;
font-weight: bold;
padding: 10px 10px 10px 10px;
margin: 0px 0px 0px 0px;
display: inline-block;
border: 1px solid #c3c0cc;
border-radius: 3px;
}
JS:
function formValidation() {
var x = document.forms['validate'];
//Username
if (x.username.value.length > 6 && x.username.value.length < 25) {
document.getElementById('username').style.borderColor = 'green';
} else {
document.getElementById('username').style.borderColor = 'red';
}
//Password
if (x.password.value.length > 6 && x.password.value.length < 25) {
document.getElementById('password').style.borderColor = 'green';
} else {
document.getElementById('password').style.borderColor = 'red';
}
//Full name
if (x.full_name.value.length > 6 && x.full_name.value.length < 25) {
document.getElementById('full_name').style.borderColor = 'green';
} else {
document.getElementById('full_name').style.borderColor = 'red';
}
//Email
if (x.email.value.length > 6 && x.email.value.length < 25) {
document.getElementById('email').style.borderColor = 'green';
} else {
document.getElementById('email').style.borderColor = 'red';
}
}
It's something small I'm missing, but I can't figure it out.
A:
Add this function
function formValidation() {
var x = document.forms['validate'];
//Username
if (x.username.value.length > 6 && x.username.value.length < 25) {
document.getElementById('username').style.borderColor = 'green';
} else {
document.getElementById('username').style.borderColor = 'red';
}
//Password
if (x.password.value.length > 6 && x.password.value.length < 25) {
document.getElementById('password').style.borderColor = 'green';
} else {
document.getElementById('password').style.borderColor = 'red';
}
//Full name
if (x.full_name.value.length > 6 && x.full_name.value.length < 25) {
document.getElementById('full_name').style.borderColor = 'green';
} else {
document.getElementById('full_name').style.borderColor = 'red';
}
//Email
if (x.email.value.length > 6 && x.email.value.length < 25) {
document.getElementById('email').style.borderColor = 'green';
} else {
document.getElementById('email').style.borderColor = 'red';
}
}
A:
Try this:
function formValidation() {
var x = document.forms['validate'];
//Username
if (x.username.value.length > 6 && x.username.value.length < 25) {
document.getElementById('username').style.borderColor = 'green';
} else {
document.getElementById('username').style.borderColor = 'red';
}
//Password
if (x.password.value.length > 6 && x.password.value.length < 25) {
document.getElementById('password').style.borderColor = 'green';
} else {
document.getElementById('password').style.borderColor = 'red';
}
//Full name
if (x.full_name.value.length > 6 && x.full_name.value.length < 25) {
document.getElementById('full_name').style.borderColor = 'green';
} else {
document.getElementById('full_name').style.borderColor = 'red';
}
//Email
if (x.email.value.length > 6 && x.email.value.length < 25) {
document.getElementById('email').style.borderColor = 'green';
} else {
document.getElementById('email').style.borderColor = 'red';
}
}