0

I have a submit button and search button in my html page. when i click submit it goes to the proper script. Now i have an search button in html, which also chacks the validation.I dont want the search button to have all this validation, only i need is to enter data in any one of this text field if search button is clicked. How can do i this using 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>
4

2 回答 2

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())"

于 2013-10-26T09:48:26.980 回答
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"> 
于 2013-10-26T11:05:27.700 回答