reyanpapa使用javascript进行表单验证

reyanpapa使用javascript进行表单验证,javascript,html,Javascript,Html,大家好,我遇到了一个关于我的html和javascript程序的错误。当我在姓名、电子邮件和年龄字段中提交输入信息时,它会提示一条错误消息,说明我必须选择性别。但是,在提示错误消息后,姓名、电子邮件和年龄中的信息已被删除。这是我的代码,希望有人能帮我 <html> <head> <!-- Creating Page Title --> <title>validation</title> <!-- For

大家好,我遇到了一个关于我的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>

验证
//启动JavaScript函数
函数验证(){
//for字段必须接受一些输入
如果(document.form1.name.value==“”){
警惕(“请说出姓名”);
document.form1.name.forus();
返回false;
}
//for字段必须接受一些输入
如果(document.form1.email.value==“”){
提醒(“请发送电子邮件”);
返回false;
}
//for字段必须接受一些输入
如果(document.form1.age.value==“”){
警惕(“请给出年龄”);
document.form1.age.focus();
返回false;
}
//年龄限制低于18岁且高于60岁时引发警报
如果(document.form1.age.value<18 | | document.form1.age.value>60){
提醒(“请给出18到60岁之间的年龄范围”);
document.form1.age.focus();
返回false;
}
//必须选择性别
如果(document.form1.gender[0]。选中==false&&
document.form1.gender[1]。选中==false){
警告(“请选择性别”);
document.form1.gender.focus();
返回false;
}
//必须至少选中一个复选框
if(document.form1.language1.checked==false&&
document.form1.language2.checked==false&&
document.form1.language3.checked==false){
警告(“请选择您的语言(至少一种)”;
返回false;
}
//必须选择国家
如果(document.form1.country.value==“”){
警惕(“请告知国家”);
document.form1.country.focus();
返回false;
}
//字段必须接受一些输入
如果(document.form1.myaddress.value==“”){
提醒(“请提供地址”);
document.form1.myaddress.focus();
返回false;
}
//字段必须接受一些输入
如果(document.form1.u_name.value==“”){
提醒(“请提供用户名”);
document.form1.u_name.focus();
返回false;
}
//字段必须接受一些输入
如果(document.form1.pass.value==“”){
警报(“请提供密码”);
document.form1.pass.focus();
返回false;
}
//密码长度必须大于5个字符
如果(document.form1.pass.value.length<6){
警报(“请提供超过5个字符的密码”);
document.form1.pass.focus();
返回false;
}
//for字段必须接受一些输入
如果(document.form1.r\u pass.value==“”){
警报(“请重新键入您的密码”);
document.form1.r_pass.focus();
返回false;
}
//密码和确认密码必须匹配
if((document.form1.pass.value)!=(document.form1.r\u pass.value)){
警报(“您的密码不匹配”);
document.form1.r_pass.value==“”;
document.form1.r_pass.focus();
返回false;
}
返回(真);
}
名称:
电子邮件:
年龄:
性别:
男性
女性
语言:
印地语
英语
乌尔都语
国家:
--挑选--
印度
巴基斯坦
蓝精灵
斯利兰卡
地址:
用户名:
密码:
重新键入密码:

这里是一个小代码。你可以自己建造

    <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>

//启动JavaScript函数
函数验证()
{
//for字段必须接受一些输入
if(document.forms[“form1”][“name”]。value==“”)
{
警惕(“请说出姓名”);
返回false;
}
}
名称:
试试看

请检查此链接-

this.form.elements["element_name"].value = 'Some Value';
<script type="text/javascript">
function submitForm(){
     if(document.forms[0].name.value=="" ){
      window.alert("name Required");
      return false;
    }
   }
</script>