Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to validate dynamically genetrated form using javascript

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 596
    Comment on it

    If you want to validate the dynamically generated form using javascript:
    You can use this code :

    HTML :

    <div id="example">
            <form id="newForm">
                <input type="text" name="email" id="email" value="mail" />
                <button type="submit">validate</button>
            </form>
        </div>
        <br/>
        <button id="newform">Create New Form</button>
    

    Javascript :

    var validateObj = {
                 rules: {
                     email: {
                         email: true
                     }
                 }
             };
    
             $(function () {
    
                 jQuery.validator.setDefaults({
                     debug: true,
                     success: "valid"
                 });
    
                 // INITIALIZE plugin on the traditional form
                 var validated = $('#newForm').validate(validateObj);
    
                 $('#newform').one('click', function () {
    
                     var newform = '<form id="newForm2"><input type="text" name="email" id="email" value="bademail" /><button type="submit">validate</button></form>'
                     $('#example').append(newform);
    
                     // INITIALIZE plugin on the new form
                     var validate = $('#newForm2').validate(validateObj);
                 });
    
             });
    

    You have to include these two files also:

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.13.1/jquery.validate.js"></script>
    

    Hope this will help you!

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: