简体   繁体   中英

html javascript Validation Error Message

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?

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";
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM