Skip to content Skip to sidebar Skip to footer

HTML With JavaScript Form Validation And JQuery Mobile

In a HTML form I am using JavaScript Form validation to avoid empty fields. This is the code for the form:

Solution 1:

Working example: http://jsfiddle.net/Gajotres/vds2U/50/

HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>jQM Complex Demo</title>
        <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
        <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi"/>
        <link  href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css" /> 
        <!--<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>-->
        <script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>    
    </head>
    <body>     
        <div data-role="page" id="index" data-theme="a" >
            <div data-role="header">
                <h3>
                    First Page
                </h3>
                <a href="#second" class="ui-btn-right">Next</a>
            </div>

            <div data-role="content">
                <form method="post" id="form1" name="form1"  action="" data-ajax="false">
                    <table align="center">
                        <tr valign="baseline">
                            <td nowrap align="right">Nickname:</td>
                            <td>
                                <input type="text" name="nickname" value="" size="32"/>
                            </td>
                        </tr>
                        <tr valign="baseline">
                            <td nowrap align="right">Email:</td>
                            <td><input type="text" name="email" value="" size="32"/></td>
                        </tr>
                        <tr valign="baseline">
                            <td nowrap align="right">Password:</td>
                            <td><input type="text" name="password" value="" size="32"/></td>
                        </tr>
                        <tr valign="baseline">
                            <td nowrap align="right">&nbsp;</td>
                            <td><input type="submit" value="Insert record"/></td>
                        </tr>
                    </table>
                    <input type="hidden" name="estado" value="0"/>
                    <input type="hidden" name="MM_insert" value="form1"/>
                </form>             
            </div>

            <div data-role="footer" data-position="fixed">

            </div>
        </div> 
        <div data-role="page" id="second" data-theme="a" >
            <div data-role="header">
                <h3>
                    Second Page
                </h3>
                <a href="#index" class="ui-btn-left">Back</a>
            </div>

            <div data-role="content">

            </div>

            <div data-role="footer" data-position="fixed">

            </div>
        </div>  
    </body>
</html>   

JavaScript:

$(document).on('submit', '#form1', function(){ 
    var x=document.forms["form1"]["nickname"].value;
    if (x==null || x=="") {
        alert("First name must be filled out");
        return false;
    } else {
        return true;    
    }
});

Changes:

  • jQuery Mobile has its own way of form handling, you need to disable it if you want to have classic form handling. It is done via attribute data-ajax="false".
  • Never use inline javascript with jQuery Mobile, I mean NEVER.

Solution 2:

For me this is working fine :

<script>
        function validateForm()
        {
            var x=document.forms["form1"]["nickname"].value;
            if (x==null || x=="")
            {
                alert("First name must be filled out");
                return false;
            }else{
                return true;
            }
        }
    </script>

Just added a else statement. It's working fine for me.


Solution 3:

I think your code should work. But If not you can make some changes as

Change 1 Remove Submit event from tag

 <form method="post" name="form1"  action="<?php echo $editFormAction; ?>">

Change 2.

Change submit button to default button and validate event here

<input type="button" onclick="javascript:validateForm();" value="Insert record">

Change 3. Now change in javascript Add code form sub

<script>
                    function validateForm()
        {
            var x=document.forms["form1"]["nickname"].value;
            if (x==null || x=="")
            {
                alert("First name must be filled out");

            }else{
                   document.forms["form1"].submit();
            }
        }
            </script>

Post a Comment for "HTML With JavaScript Form Validation And JQuery Mobile"