BS in the project development, we often need to judge the data, in a specific situation, the number of words to limit the input, recently encountered a problem.
usually in the background for data validation, but the background verification has a certain, that is, the response is relatively slow, the code is as follows.
if (TxtCheckTest.Text.Count () >10) { modelstate.addmodelerror ("Readdescription", "reading items cannot exceed 10 characters!") ");}
The effect is as follows:
The biggest problem is that customers in the input process, in fact, do not know how many words entered, wait until the submission of the input content does not meet the requirements, so that the user experience is not good. If we in the user input process to give timely reminders, then, it can be very convenient to feedback the message to the customer, increase user experience degree.
The front code is as follows:
<scriptlanguage= "JavaScript" type= "Text/javascript" >//Set Prompt function Setpromptandformat for text boxes (ID, Prefi X,suffix, defaultvalue) {var Inputctrl = $ ("#" +id); var formatprompt = prefix +defaultvalue + suffix; if (Inputctrl.val (). length < 1) {//inputctrl.val (formatprompt); Inputctrl.addclass ("prompt"); } inputctrl.blur (function () {if (Inputctrl.val (). Length <1) {//inputctrl . val (formatprompt); Inputctrl.addclass ("prompt"); } else {inputctrl.val (prefix +inputctrl.val () + suffix); } }); Inputctrl.focus (function () {if (Inputctrl.val () ==formatprompt && inputctrl.attr ("class") = = "Prom PT ") {Inputctrl.removeclass (" prompt "); Inputctrl.val (""); } else {var removeprefix =inputctrl.val (). substring (prefix.length, Inputctrl.val (). length); var removesuffix =removeprefix.substr (0, removeprefix.lastindexof (suffix)); Inputctrl.val (Removesuffix); } }); }$ (document). Ready (function () {Setpromptandformat ("Data_title", "about", "confirmation of matters", "* * *"); Settextareamaxlength ("Data_title", 30); Settextareamaxlength ("Data_abstract", 800); Settextareamaxlength ("Data_explain", 100); Settextareamaxlength ("Data_description", 300); }); </script>
In this way, when the user enters a number beyond the limit, it will not be able to continue to input, can maximize the savings in customer time, improve the experience.
JavaScript for data validation