-2

我希望当用户从字段中输入用户名时,它会从 process.php 进行验证,并在验证后将结果显示在名称字段的前面。我希望当用户从 contry 中选择 uk 时,它显示英国城市,如果选择印度,则显示印度城市如何使用 jquery 以及如果任何字段 eppty 显示错误

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html>
<head>
    <title>JQuery Form Example</title> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
    <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        $("#myform").validate({
            debug: false,
            rules: {
                name: "required",
                email: {
                    required: true,
                    email: true
                }
            },
            messages: {
                name: "Please let us know who you are.",
                email: "A valid email will help us get in touch with you.",
            },
            submitHandler: function(form) {
                // do other stuff for a valid form
                $.post('process.php', $("#myform").serialize(), function(data) {
                    $('#results').html(data);
                });
            }
        });
    });
    </script>
    <style>
    label.error { width: 250px; display: inline; color: red;}
    </style>
</head>
<body>
<form name="myform" id="myform" action="" method="POST">  
<!-- The Name form field -->
    <div id='name'> </div>
    <label for="name" id="name_label">Username</label>  
    <input type="text" name="name" id="name" size="30" value=""/>  
    <br>
<!-- The Email form field -->
    <div id='email'> </div>
    <label for="email" id="email_label">Email</label>  
    <input type="text" name="email" id="email" size="30" value=""/> 
    <br>
<!-- The Country form field -->
    <div id='Select_option'> </div>
    <label for="country" id="country_label">Contry</label>  
<select name="contry">
<option value="uk">uk</option>
<option value="india">india</option>
</select>

    <br>
<!-- The Country form field -->
    <div id='uk_city'> </div>
    <label for="ukcity" id="ukcity_label">uk city</label>  
<select name="uk_city">
<option value="u0">u0</option>
<option value="u1">u1</option>
</select>

    <br>
<!-- The Country form field -->
    <div id='ind_city'> </div>
    <label for="indcity" id="indcity_label">ind city</label>  
<select name="ind_city">
<option value="i0">i0</option>
<option value="i1">i1</option>
</select>

    <br>


<!-- The Submit button -->
    <input type="submit" name="submit" value="Submit"> 
</form>
<!-- We will output the results from process.php here -->
<div id="results"><div>
</body>
</html>
4

1 回答 1

1

First, most validation should be done i js to save server roundtrips for easy things like empty fields or invalid input (letters in an age field).

Then use ajax to validate the form returning a json object containing either success = true or success = false along with an array with objects indicating which fields are in error and what the error is.

You could avoid posting the form bu using $.ajax command instead and only sending the fields you need to validate.

Also remember to not trust js validation, only server side validation is immune to tampering.

Normally you create a server side session where you set logged in to true to prevent the user to bypass login bu going directly to specific page.

于 2012-05-18T09:32:40.427 回答