-1

大家好,我的 html 和 javascript 程序遇到错误。当我在姓名、电子邮件和年龄字段中提交输入信息时,它会提示错误消息,说我必须选择性别。但是,在提示错误信息后,姓名、电子邮件和年龄中的信息已被删除。这是我的代码,我希望有人可以帮助我。

<html>
<head>
    <!-- Creating Page Title -->
    <title>validation</title>

    <!-- For Form validation we have to use JavaScript
    JavaScript needs only a browser to run, there is no need of 
    server to run JavaScript -->

    <script language="javascript">

        //Start JavaScript Function

        function verify() {

            //for field must take some input

            if (document.form1.name.value == "") {
                alert("Please give the name");
                document.form1.name.forus();
                return false;
            }

            //for field must take some input

            if (document.form1.email.value == "") {
                alert("Please give the email");
                return false;
            }

            // for field must take some input

            if (document.form1.age.value == "") {
                alert("Please give age");
                document.form1.age.focus();
                return false;
            }

            // alert thrown when age limit is below 18 and above 60

            if (document.form1.age.value < 18 || document.form1.age.value > 60) {
                alert("Please give Age range between 18 and 60");
                document.form1.age.focus();
                return false;
            }

            // Gender must be selected

            if (document.form1.gender[0].checked == false &&
                    document.form1.gender[1].checked == false) {
                alert("Please select gender");
                document.form1.gender.focus();
                return false;
            }

            // At least one checkbox must be checked

            if (document.form1.language1.checked == false &&
                    document.form1.language2.checked == false &&
                    document.form1.language3.checked == false) {
                alert("Please Select your choice of language(Atleast One)");
                return false;
            }

            //Country must be chosed

            if (document.form1.country.value == "") {
                alert("Please give country");
                document.form1.country.focus();
                return false;
            }

            //field must take some input

            if (document.form1.myaddress.value == "") {
                alert("Please give address");
                document.form1.myaddress.focus();
                return false;
            }

            //field must take some input

            if (document.form1.u_name.value == "") {
                alert("Please give username");
                document.form1.u_name.focus();
                return false;
            }

            //field must take some input

            if (document.form1.pass.value == "") {
                alert("Please give Password");
                document.form1.pass.focus();
                return false;
            }

            //password length must be greater than 5 characters

            if (document.form1.pass.value.lenght < 6) {
                alert("Please give a Password more than 5 characters");
                document.form1.pass.focus();
                return false;
            }

            // for field must take some input

            if (document.form1.r_pass.value == "") {
                alert("Please retype your password");
                document.form1.r_pass.focus();
                return false;
            }

            //password and confirm password must matched

            if ((document.form1.pass.value) != (document.form1.r_pass.value)) {
                alert("Your password does not match");
                document.form1.r_pass.value == "";
                document.form1.r_pass.focus();
                return false;
            }
            return( true );
        }
    </script>

</head>
<body>

<!-- Creating Form -->
<form method="POST" action="" name="form1">

    <!-- Creating Table, having 11rows and 2 columns. -->
    <table border="2" align="center" cellpadding="7">

        <!-- Start First Row -->
        <tr>
            <!-- Creating First Column -->
            <td><strong>Name:</strong></td>

            <!-- Creating Second Columns -->
            <td>
                <!-- TextBox -->
                <input type="text" name="name"/>
            </td>
            <!-- Close First Row -->
        </tr>

        <!-- Creting First Columns -->
        <td><strong>Email:</strong></td>

        <!-- Creating Second Columns -->
        <td>
            <!-- Textbox -->
            <input type="text" name="email"/>
        </td>
        <!-- Close Second row -->
        </tr>

        <tr>
            <td><strong>Age:</strong></td>
            <td>
                <!-- Textbox -->
                <input type="text" name="age" size="2"/>
            </td>
        </tr>

        <tr>
            <td><strong>Gender:</strong></td>
            <td>
                <!-- Radio Butong -->
                <input type="radio" name="gender" value="Male"/>Male
                <input type="radio" name="gender" value="Female"/>Female

            </td>
        </tr>

        <tr>
            <td><strong>Language:</strong></td>
            <td>
                <!-- Check box -->
                <input type="checkbox" name="language1" value="Hindi"/>Hindi
                <input type="checkbox" name="language2" value="English"/>English
                <input type="checkbox" name="language3" value="Urdu"/>Urdu
            </td>
        </tr>

        <tr>
            <td><strong>Country:</strong></td>
            <td>
                <!-- Combo Box -->
                <select name="country"/>
                <option value="" selected/>
                --Select--
                <option value="indi"/>
                India
                <option value="pakistan"/>
                Pakistan
                <option value="beangladesh"/>
                Beangladesh
                <option value="srilanka"/>
                Srilanka
                </select>
            </td>
        </tr>

        <tr>
            <td><strong>Address:</strong></td>
            <td>
                <!-- TextArea -->
                <textarea rows="5" cols="20" name="myaddress"/></textarea>
            </td>
        </tr>

        <tr>
            <td><strong>Username:</strong></td>
            <td>

                <!-- Textbox -->
                <input type="text" name="u_name"/>
            </td>
        </tr>

        <tr>
            <td><strong>Password:</strong></td>
            <td>
                <!-- Password Field -->
                <input type="password" name="pass"/>
            </td>
        </tr>

        <tr>
            <td><strong>Retype Password:</strong></td>
            <td>
                <!-- Password Field -->
                <input type="password" name="r_pass"/>
            </td>
        </tr>

        <tr align="center">

            <td>
                <!--Submit Button, Function verify need to be called when we process
                submit button-->
                <input type="submit" value="Submit" onClick="return (verify());"/>
            </td>

            <td>
                <!--Reset Button-->
                <input type="reset">
            </td>
        </tr>

        <!--Table Close-->
    </table>

    <!--Form Close -->
</form>
</body>
</html>
4

3 回答 3

0

这是一个小代码。你可以自己构建它

    <script type="text/javascript">

//Start JavaScript Function

function verify()
{

//for field must take some input

if(document.forms["form1"]["name"].value=="")
{
alert("Please give the name");

return false;
}
}
</script>
    <form method="POST" action="test3.asp" name="form1" onSubmit="return verify()">

<!-- Creating Table, having 11rows and 2 columns. -->
<table border="2" align="center" cellpadding="7">

<!-- Start First Row -->
<tr>
<!-- Creating First Column -->
<td><strong>Name:</strong></td>

<!-- Creating Second Columns -->
<td>
<!-- TextBox -->
<input type ="text" name="name"/>
<input type="submit" value="Submit" />
</form>

试试看

于 2012-11-03T10:47:46.073 回答
0

请检查此链接 - http://www.w3schools.com/js/js_form_validation.asp

在这里您可以找到空白字段和​​电子邮件验证。

为了保留字段数据,您可以使用 cookie/session 或者如果您只是在页面验证中使用,您可以通过 JavaScript 代码直接设置它

this.form.elements["element_name"].value = 'Some Value';

此处,element_name 应替换为当前验证字段名称,Some Value 应为当前验证字段的当前值。

在返回 false 之前使用此代码。

谢谢

于 2012-11-03T11:01:49.600 回答
0
<script type="text/javascript">
function submitForm(){
     if(document.forms[0].name.value=="" ){
      window.alert("name Required");
      return false;
    }
   }
</script>
于 2013-03-28T03:39:33.777 回答