0

Using the HTML "required" indicator for a field displays a very nice error message when there is no entry in the field showing "Please fill out this field" and pointing to the field, and message clears after about 5 secs. Using javascript for additional validation provides an unattractive alert box with a message requiring user intervention. Is there a way for javascript to display an HTML message for the field or similar, or do I have to use eg. jQuery for this kind of functionality?

1 Answer 1

1

You can have a hidden div or span and then on validation of your form just set the text and display of that hidden field to be the error message you want.

function doValidate() {
   var myField = document.getElementById("textbox1");
   if ( myField.value.length == 0 ) {  // fail
     document.getElementById("hiddenDiv").style.display = '';
     document.getElementById("hiddenDiv").innerHTML = "Fail";
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for that. How do I show the error message in red for eg. <TD ID="error_password"></TD> which is the area for the error message?
It's OK, I eventually found it - "style=color:red".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.