javascript validation after dust template renders

414 Views Asked by At

I have been able to load the page using dust template but I have go give some kind of validation to the forms. How do I add the validation ?

Do I need to use Dust Helper method only ?

1

There are 1 best solutions below

1
On

No you don't need to use a Dust Helper to validate a form however it could be an option but not necessarily a common solution. You can do it simply in your JavaScript after you render the page.

Here is a simple example:

dust.render('myTemplate', json, function(err, out){
  // generating the html here
  $('#my-form').on('submit', validateForm);
});

function validateForm(e) {
  e.preventDefault();
  // validation logic goes here
}
<div class="page-content">
  <form action="action.php" method="post" id="my-form">
    <input type="text" name="username" />
    <input type="password" name="password" />
    <button>Submit</button>
  </form>
</div>