如何通过两个提交按钮清除验证问题?

时间:2013-10-26 09:37:18

标签: javascript html cgi

我的html页面中有一个提交按钮和搜索按钮。当我点击提交时,它会转到正确的脚本。现在我在html中有一个搜索按钮,它也会查询验证。我不希望搜索按钮具有所有这些验证,只有我需要的是在单击搜索按钮的任何一个文本字段中输入数据。我怎么能用javascript ???

<!DOCTYPE html>
<html>
<head>
    <title>INFORMATION</title>
<script type="text/javascript">

function validate() {

    if ( document.myForm.firstname.value=="" ){
    alert( "Please provide the firstname" );
    return false;   
    }

    if ( document.myForm.lastname.value=="" ){
    alert( "Please provide the lastname" );
    return false;
    }

    if ( document.myForm.age.value=="" || isNaN(document.myForm.age.value) ||     document.myForm.age.value<1){
    alert( "Please provide the valid age in Integer" );
    return false;
    }

    if ( document.myForm.gender.value=="" ){
    alert( "Please provide the gender" );
    return false;
    }

}      
</script>
</head>
    <body>
        <form action = "/~neeraj/cgi-bin/test.py" name="myForm" method="post"     onsubmit="return(validate())">
            <table cellspacing="3" cellpadding="2" border="1">
                <tr>
                    <td> FirstName:* </td>
                    <td> <input type="text" name="firstname" /></td>
                </tr>

                <tr>
                    <td>LastName:* </td>
                    <td> <input type="text" name="lastname" /></td>
                </tr>

                <tr>
                    <td>Age:* </td>
                    <td><input type="text" name="age" /></td>
                </tr>

                <tr>
                    <td>Gender:* </td>
                    <td><input type="radio" name="gender" value="male" /> Male
                        <input type="radio" name="gender" value="female" /> Female</td>
                </tr>
            </table>

                <br>
                <input type="submit" name="submit "value="SUBMIT">
                <input type="reset" name="reset" value="RESET">
                <input type="submit" name="search" formmethod="post"     formaction="/~neeraj/cgi-bin/search.py" value="SEARCH">
        </form>

   </body>
</html>

2 个答案:

答案 0 :(得分:0)

<!DOCTYPE html>
 <html>
  <head>
  <title>INFORMATION</title>
   <script type="text/javascript">

 function validate()
 {
if ( document.myForm.firstname.value=="" ) 
{
alert( "Please provide the firstname" );
return false;   
}  
else if ( document.myForm.lastname.value=="" )
{
alert( "Please provide the lastname" );
return false;
}  
else if ( document.myForm.age.value=="" || isNaN(document.myForm.age.value) ||     document.myForm.age.value<1)
{
alert( "Please provide the valid age in Integer" );
return false;
}
else if ( document.myForm.gender.value=="" )
{
 alert( "Please provide the gender" );
 return false;
}
}      
</script>
</head>
<body>
    <form action = "/~neeraj/cgi-bin/test.py" name="myForm" method="post">
        <table cellspacing="3" cellpadding="2" border="1">
            <tr>
                <td> FirstName:* </td>
                <td> <input type="text" name="firstname" /></td>
            </tr>

            <tr>
                <td>LastName:* </td>
                <td> <input type="text" name="lastname" /></td>
            </tr>

            <tr>
                <td>Age:* </td>
                <td><input type="text" name="age" /></td>
            </tr>

            <tr>
                <td>Gender:* </td>
                <td><input type="radio" name="gender" value="male" /> Male
                    <input type="radio" name="gender" value="female" /> Female</td>
            </tr>
        </table>

            <br>
            <input type="submit" name="submit1" value="SUBMIT" onclick="return(validate())">
            <input type="reset" name="reset1" value="RESET">
            <input type="submit" name="search" value="SEARCH">
    </form>
  </body>
</html>

onclick使用按钮onclick="return(validate())"

答案 1 :(得分:0)

只需更改以下代码段:

<form action = "/~neeraj/cgi-bin/test.py" name="myForm" method="post">
    :
<input type="submit" name="submit" value="ADD" onclick="return(validate())">
<input type="reset" name="reset" value="RESET">                         
<input type="submit" name="search" formmethod="post" formaction="/~neeraj/cgi-bin    /search.py" value="SEARCH">