mvasco
mvasco

Reputation: 5107

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:

 <form method="post" name="form1" onsubmit="return validateForm()" action="<?php echo $editFormAction; ?>">
        <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>

And this is the JavaScript function:

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

As expected, if the user clicks on the submit button and the field 'nickname' is empty, the Alert Dialog is shown, but after closing it, the form is submitted. I am using Dreamweaver as editor and JQuery Mobile in my web, may be this is the problem.

Any help is welcome.

Upvotes: 0

Views: 147

Answers (3)

Gajotres
Gajotres

Reputation: 57309

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 rel="stylesheet" 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.

Upvotes: 3

Ravindra patankar
Ravindra patankar

Reputation: 113

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>

Upvotes: -2

mike20132013
mike20132013

Reputation: 5425

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.

Upvotes: 2

Related Questions