0

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.

4
  • 2
    its working fine on chrome.. Commented May 23, 2014 at 4:17
  • @user3091574, thank you, I am using Safari and Chrome, on both browsers the Alert Dialog is shown, but after closing it, the form is submitted. Commented May 23, 2014 at 4:19
  • @user3091574, this is the problem, it does, but I guess the problem lies on the JQuery Mobile side of my web and not on the JavaScript form validation. Commented May 23, 2014 at 4:25
  • 1
    I fixed your problem, take a loot at my answer below. Commented May 23, 2014 at 8:00

3 Answers 3

3

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.
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, it was, after inserting data-ajax="false" the form is not submitted until the field is not empty. Thank you again.
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.

1 Comment

Thank you, with or without putting the else statement, the javascript is working fine, but after closing the alert dialog, the form is submitted in both cases. I am sure the problem is at the JQuery Mobile.
-2

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>

1 Comment

Thank you, but there is a better answer to my question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.