javascript - How can I call to HTML DOM Event Object (for example: onclick, onblur, etc..) nested functions? -


good day! let's assume have group of textfields in page, each of textfield has own validation during onblur, example amount textfield, if remove focus on it, check if greater 100,000.

<input type = "text" id = "amount" placeholder="amount" onblur="validateamount()">

function validateamount(){   var amount = document.getelementbyid("amount").value;   if (amount > 100000){       document.getelementbyid("errormessage").innerhtml = "invalid! enter number less 100,000!"; //let's assume have label error message   } } 

oops! 1 of textfields has validation, if have 5, let's assume click button process data 3 problems on validation, data not processed because of errors. in case i'm going use onclick event, take of example:

<button onclick = "checkdata()"> apply </button>

function checkdata(){ //here in checkdata() method/function, want put validateamount() function above , other functions of validation during onblur count , state number of errors before submitting data.      var numberoferrors = 0;     function validateamount(){       var amount = document.getelementbyid("amount").value;       if (amount > 100000){           document.getelementbyid("errormessage").innerhtml = "invalid! enter number less 100,000!"; //let's assume have label error message           numberoferrors++;       }     }     /* , more validating functions here     */      // after validating textfields.     if(numberoferrors == 0){     alert("congrats! submitted loan");     } else {     alert("error! check inputs!");     } } 

so question how can call function inside function on html dom events? there possible , easier way this? thank in advance!

there many approaches this, recommend using validation library. rolling own risky proposition, , lead unmanageable "spaghetti code".

here couple of libraries can into.

http://jqueryvalidation.org

https://angularjs.org


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -