Hide the javascript innerHTML after error is corrected - javascript

I have used the following javascript code to add inline error in my form. I want to error to be removed after the error is corrected after each field is corrected. I have created a single function for all the validations, so not able to use else and remove the error manually.
var emailpattern = /^[a-zA-Z][a-zA-Z0-9_]*(\.[a-zA-Z0-9_]+)*#[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.([a-zA-Z]{2,4})$/
var passwordpattern = /^(?=.*[a-z])(?=.*[A-Z])(?=.*[$#$!%*?&])[A-Za-z\d$#$!%*?&]{8,}$/;
function validateForm() {
var x = document.forms["LoginForm"]["email"];
if (x.value == "") {
x.value = "";
document.getElementById('pointemail').innerHTML = "Please enter the email id.";
x.focus();
return false;
} else if (!emailpattern.test(x.value)) {
x.value = "";
document.getElementById('pointemail').innerHTML = "Please enter a valid email address.";
x.focus();
return false;
}
x = document.forms["LoginForm"]["password"];
if (x.value == "") {
x.value = "";
document.getElementById('pointpassword').innerHTML = "Please enter the password.";
x.focus();
return false;
} else if (!passwordpattern.test(x.value)) {
x.value = "";
document.getElementById('pointpassword').innerHTML = "Password should have minimum 8 characters, one upper case, one lower case and one special character";
x.focus();
return false;
}
}

Try This
var emailpattern = /^[a-zA-Z][a-zA-Z0-9_]*(\.[a-zA-Z0-9_]+)*#[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.([a-zA-Z]{2,4})$/
var passwordpattern = /^(?=.*[a-z])(?=.*[A-Z])(?=.*[$#$!%*?&])[A-Za-z\d$#$!%*?&]{8,}$/;
function validateForm() {
var x = document.forms["LoginForm"]["email"];
document.getElementById('pointemail').innerHTML = "";
document.getElementById('pointpassword').innerHTML = "";
//Add your if else here.
}
Hope it will be useful. :)

Provide an extra else statement after elseif
else {
document.getElementById('pointemail').innerHTML = '';
}
Similarly for pointPassword

Related

JavaScript display multiple error message form submission?

What am I doing wrong in my JavaScript? I would like to display an error message if a user forgets to type in any of my HTML form fields. I would like to create an error message for the name, email, and phone number fields. Even if a user puts in their name, I would like error messages for the remainder 2, or remainder 1, or no error messages. I have attached my JavaScript code below. Thank you for those who help.
function validateForm() {
var ret = true;
var name = document.forms["contactform"]["name"].value;
if (name == "") {
document.getElementById('error').innerHTML = "Please enter your name";
ret = false;
}
var email = document.forms["contactform"]["email"].value;
if (email == "") {
document.getElementById('erroremail').innerHTML = "Please enter your email";
ret = false;
}
var phone = document.forms["contactform"]["phone"].value;
if (phone == "") {
document.getElementById('errorphone').innerHTML = "Please enter your phone";
ret = false;
}
return ret;
}
If you want separate error display for each input you could just add else with empty innerHTML.
Something like this:
var name = document.forms["contactform"]["name"].value;
var nameError = document.getElementById('error');
if (name == "") {
nameError.innerHTML = "Please enter your name";
ret = false;
} else {
nameError.innerHTML = "";
}
Full example here: https://jsfiddle.net/1h1tkh7y/
Create a variable called something like :
var fieldsInError = "";
Then in your validation in each check if it fails validation add the field:
fieldsInError += "name," (or whatever the field is)
Then at the end of the function update your error field with something like:
document.getElementById('error').innerHTML = "Please enter " + fieldsInError.substring(0, fieldsInError.length-1);
So your function would look something like this:
function validateForm() {
var fieldsInError = "";
var name = document.forms["contactform"]["name"].value;
if (name == "") {
fieldsInError += "name,"
}
var email = document.forms["contactform"]["email"].value;
if (email == "") {
fieldsInError += "email,"
}
var phone = document.forms["contactform"]["phone"].value;
if (phone == "") {
fieldsInError += "phone,"
}
if(fieldsInError != ""){
document.getElementById('error').innerHTML = "Please enter " + fieldsInError.substring(0, fieldsInError.length-1);
return false;
} else {
return true;
}
}

If Statement Only Executing Else Part

I have a simple program that validates a sign up form. Basically I reach a problem when one of my "if" statements can only execute the "else" part. If I change the form so that "if" is executed, nothing happens. Below is my code:
function verifyprocedure() {
var originalpassword = document.getElementById("password").value;
var checkpassword = document.getElementById("verifypassword").value;
var emailcheck = document.getElementById("email").value;
var male = document.getElementById("gendermale");
var female = document.getElementById("genderfemale");
var form = document.getElementById("signup");
var emailexist = /^[a-zA-Z0-9._-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
if (originalpassword == checkpassword) {
if (originalpassword.length < 9) {
if (emailexist.test(emailcheck)) //this is the statement that does not work
{
alert("Hello World!"); //this is whats supposed to be executed
} else { //this is successfully executed
$("#email").notify("Please enter a valid email address.");
}
} else {
$("#password").notify("Passwords must have at least 9 characters.");
}
} else {
$("#verifypassword").notify("Passwords do not match.");
}
}
Did you check whether it is working or not ?
anyway it is working for me,i think your input should be invalid you can use the following code for checking
var originalpassword ="abcdefgh";
var checkpassword = "abcdefgh";
var emailcheck = "arun#gmail.com";
var male ="male";
var emailexist = /^[a-zA-Z0-9._-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
if (originalpassword == checkpassword) {
alert("hai");
}
if (originalpassword.length < 9) {
if (emailexist.test(emailcheck))
{
alert("Hello World!");
} else {
alert("Please enter a valid email address.");
}
}
Working DEMO
UPDATE
var originalpassword ="abcdefgh";
var checkpassword = "abcdefgh";
var emailcheck = "arun#gmail.com";
var male ="male";
var emailexist = /^[a-zA-Z0-9._-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
if (originalpassword == checkpassword)
{
alert("hai");
if (originalpassword.length > 9)
{
if (emailexist.test(emailcheck))
{
alert("Hello World!");
}
else
{
alert("Please enter a valid email address.");
}
}
else
{
alert("Passwords must have at least 9 characters.");
}
}
else {
alert("Passwords do not match.");
}
Check this DEMO It will satisfy all conditions.
Note : Check your if condition for password length, if you want the desired output then it will be like if (originalpassword.length > 9)
if (originalpassword.length > 9) {
if (emailexist.test(emailcheck)) //this is the statement that does not work
{
alert("Hello World!"); //this is whats supposed to be executed
} else { //this is successfully executed
$("#email").notify("Please enter a valid email address.");
}
} else {
$("#password").notify("Passwords must have at least 9 characters.");
}

How to show the validation messages together

I need your assistant and help in modifying the below code to show the validation messages if not satisfied together not one by one. My code is:
function validateForm()
{
var x = "";
var y = "";
if ( (RFC.C1.checked) && (!RFC.Language_e.checked || !RFC.Language_e.checked ))
{
y += "Please Select the Language";
document.getElementById('error').innerHTML = y;
return false;
}
else if (document.RFC.Reason.value=="")
{
x += "Please enter a Reason";
document.getElementById('error').innerHTML = x;
return false;
}
else
{
return true;
}
}
In addition, the validation message is shown one time and if the user didn't correct it and submit the page, the error will not be shown again. Can you please help in the two issues?
I would do something like this:
function validateForm() {
var error = "";
var valid = true;
if ( (RFC.C1.checked) && (!RFC.Language_e.checked || !RFC.Language_e.checked )) {
error += "<li>Please Select the Language</li>";
valid = false;
}
else if (document.RFC.Reason.value=="") {
error += "<li>Please enter a Reason</li>";
valid = false;
}
if(valid === false) {
document.getElementById('error').innerHTML = "<ul>" + error + "</ul>";
}
return valid;
}
Get all your errors and store them.
Change the valid boolean to false if any single or multiple error occurs
Only display errors if they happen.
Link to the error locations in your page for easy navigation.
function validateForm()
{
var x = "";
var y = "";
var z = "";
if ( (RFC.C1.checked) && (!RFC.Language_e.checked || !RFC.Language_e.checked) )
{
y += "Please Select the Language";
document.getElementById('error').innerHTML = y;
return false;
}
else if ( document.RFC.Reason.value=="" )
{
x += "Please enter a Reason";
document.getElementById('error').innerHTML = x;
return false;
}
else if ( ((RFC.C1.checked) && (!RFC.Language_e.checked || !RFC.Language_e.checked )) && (document.RFC.Reason.value=="") )
{
z += "Please enter a Reason and Language";
document.getElementById('error').innerHTML = z;
return false;
}
else
{
return true;
}
}
By the way your code is working but not nice. You should read something about programming style. To get nice validation quicly you can use jQuery Validate plug-in.

Form Validation with a bit of elegance

Currently I have got the following code and I keep hearing that I should not use the alert function, it's old fashioned etc.
What else could I use instead of alert?
document.getElementById("practiseForm").onsubmit = function() {
if(document.getElementById("fname").value.trim() === ""){
alert("First Name Field Cannot Be Blank");
allowsubmit = false;
}
if(document.getElementById("lname").value.trim() === ""){
alert("Last Name Field Cannot Be Blank");
allowsubmit = false;
}
var email = document.getElementById('email');
var emailRegEx = /[-\w.]+#([A-z0-9][-A-z0-9]+\.)+[A-z]{2,4}/;
if (!emailRegEx.test(email.value)) {
alert("Invalid Email address");
return false;
}
}
Fiddle
I updated your fiddle to provide an example of inline error messages:
document.getElementById("practiseForm").onsubmit = function() {
var fName = document.getElementById("fname");
var fNameError = fName.nextElementSibling;
var lName = document.getElementById("lname");
var lNameError = lName.nextElementSibling;
if(fName.value.trim() === ""){
fNameError.innerHTML = "First Name Field Cannot Be Blank";
allowsubmit = false;
} else {
fNameError.innerHTML = "";
}
if(document.getElementById("lname").value.trim() === ""){
lNameError.innerHTML = "Last Name Field Cannot Be Blank";
allowsubmit = false;
} else {
lNameError.innerHTML = "";
}
var email = document.getElementById('email');
var emailError = email.nextElementSibling;
var emailRegEx = /[-\w.]+#([A-z0-9][-A-z0-9]+\.)+[A-z]{2,4}/;
if (!emailRegEx.test(email.value)) {
emailError.innerHTML = "Error in e-mail format";
return false;
} else {
emailError.innerHTML = "";
}
}
http://jsfiddle.net/Ty8AQ/10/
There are so many possiblities here, and so many examples on the web.
A possibility is creating an 'error-div' where you put all the error messages in. If you want you can style the div (color red etc etc)
HTML
<ul class="error-messages">
</ul>
JS
var li = document.createElement("li");
li.innerHTML = "Insert error message here";
document.querySelector("#ul.error-messages").appendChild(li);
CSS
.error-messages{color: red}
Another possibility are inline error messages next to the text box
Error messages when you hover on the input field
...

innerHTML is not hiding in javascript validation

function editvalidation() {
var isDataValid = true;
var currentCourseO = document.getElementById("currentCourseNo");
var newCourseNoO = document.getElementById("newCourseNo");
var currentCourseMsgO = document.getElementById("currentAlert");
var newCourseMsgO = document.getElementById("newAlert");
if (currentCourseO.value == "") {
currentCourseMsgO.innerHTML = "Please Select a Course to edit from the Course Drop Down Menu";
newCourseMsgO.innerHTML = "";
isDataValid = false;
} else {
currentCourseMsgO.innerHTML = "";
}
if (newCourseNoO.value == "") {
newCourseMsgO.innerHTML = "Please fill in the Course ID in your Edit";
isDataValid = false;
} else {
newCourseMsgO.innerHTML = "";
}
return isDataValid;
}
Hi, in the code above what I am trying to do is that if the currentCourseO.value == "" is met, then display its string message but do not display the string message for newCourseMsgO.
If currentCourseO.value == "" is not met then display the string for newCourseMsgO which is newCourseMsgO.innerHTML = "Please fill in the Course ID in your Edit"; if this validation is met.
At the moment it is not hiding the string for newCourseMsgO when currentCourseO.value == "" is met. Can I please have answer in javascript please.
It sounds like your two if-else statements should be connected, right now they are not dependent on one another. Try this:
if (currentCourseO.value == "") {
currentCourseMsgO.innerHTML = "Please Select a Course to edit from the Course Drop Down Menu";
newCourseMsgO.innerHTML = "";
isDataValid = false;
} else {
if (newCourseNoO.value == "") {
newCourseMsgO.innerHTML = "Please fill in the Course ID in your Edit";
isDataValid = false;
} else{
newCourseMsgO.innerHTML = "";
}
}

Categories