用于验证jsp表单的Javascript代码不是验证

时间:2014-01-29 09:49:21

标签: javascript jsp validation

你好我在WebContent中有一个js文件,它有一个函数validate()来验证WEB-INF中Signup.jsp的代码。

  1. 如何将该功能添加到表单中的“onsubmit”?
  2. 我尝试在Signup.jsp中编写javascript并在“onsubmit ='return validate'”中调用它,但它无效。
  3. 所以有人帮我帮忙。

    这是我的注册表单

      '<form name="Register" action="RegisterServlet" method="post" onSubmit="return validate()">
    <table>
    <tr>
    <td>First Name* : </td>
    <td><input type="text" name="txtFname" id="fname" maxlength="30"/>
    <span id="errorFirstNameMissing" style="visibility:hidden;">*Please provide your first name.</span>
    <span id="errorFirstNameInValid" style="visibility:hidden;">*Please provide a valid first name.</span>
    </td>
    </tr>
    <tr>
    <td>Last Name* : </td>
    <td><input type="text" name="txtLname" id="lname" maxlength="30"/>
    <span id="errorLastNameMissing" style="visibility:hidden;">*Please provide your first name.</span>
    <span id="errorLastNameInValid" style="visibility:hidden;">*Please provide a valid first name.</span>
    </td>
    </tr>
    <tr>
    <td>Gender* : </td>
    <td><select name = "txtGender" id="gender">
    <option value="unknown">Select your Gender</option>
    <option value="Male">Male</option>
    <option value="Female">Female</option>
    </select>
    <span id="errorMissingGender" style="visibility:hidden;">*Please provide your first name.</span>
    
    </td>
    </tr>
    <tr>
    <td>Category* : </td>
    <td><select name = "txtCategory" id="category">
    <option value="unknown">Select your Category</option>
    <option value="Affiliate">Affiliate</option>
    <option value="Client">Client</option>
    <option value="Admin">Admin</option>
    </select>
    <span id="errorMissingCategory" style="visibility:hidden;">*Please provide your first name.</span>
    
    </td>
    </tr>
    <tr>
    <td><script type="text/javascript"
    src="js/Calendar.js"></script>
    <span id="errorMissingDOB" style="visibility:hidden;">*Please provide your first name.</span>
    
    </td>
    </tr>
    <tr>
    <td>Age :</td>
    <td><input type=text name=txtAge id="ageId" readonly  style="width: 20px;background-color:#D0D0D0;border:none"/>yrs.</td>
    </tr>
    
    <tr>
    <td>Address* : </td>
    <td><textarea rows="5" name="txtAddr" id="addr" cols="30"></textarea>
    <span id="errorMissingAddress" style="visibility:hidden;">*Please provide your first name.</span>
    </td>
    </tr>
    <tr>
    <td>State* :</td>  
    <td> <select onchange="print_city('city',this.selectedIndex);" id="state" name ="txtState"></select>
    <span id="errorMissingState" style="visibility:hidden;">*Please provide your first name.</span>
    
    </td>
    </tr>
    <tr>
    <td>City* :</td> <td><select name ="txtCity" id ="city"></select>
            <script language="javascript">print_state("state");</script>
    <span id="errorMissingCity" style="visibility:hidden;">*Please provide your first name.</span>
    </td>
    </tr>
    
    <tr>
    <td>Pincode* : </td>
    <td><input type="text" name="txtPin" id="pin"/>
    <span id="errorMissingPinCode" style="visibility:hidden;">*Please provide your first name.</span>
    <span id="errorPinCodeInvalid" style="visibility:hidden;">*Please provide your first name.</span>
    
    </td>
    </tr>
    <tr>
    <td>e-Mail* : </td>
    <td><input type="text" name="txtEmail" id="email"/>
    <span id="errorMissingEmail" style="visibility:hidden;">*Please provide your first name.</span>
    <span id="errorEmailInvalid" style="visibility:hidden;">*Please provide your first name.</span>
    </td>
    </tr>
    <tr>
    <td>Contact Number : </td>
    <td><input type="text"  name="txtStd" id="std" maxlength="6" style="width: 40px"/>-<input type="text" name="txtPhone" id="phone" maxlength="10"/>
    <span id="errorStdCodeInvalid" style="visibility:hidden;">*Please provide your first name.</span>
    <span id="errorPhoneNoInvalid" style="visibility:hidden;">*Please provide your first name.</span>
    </td>
    </tr>
    
    <tr>
    <td>Mobile Number* : </td>
    <td>+91-<input type="text" name="txtMobile" id="mobile" maxlength="10"/>
    <span id="errorMissingMobileNo" style="visibility:hidden;">*Please provide your first name.</span>
    <span id="errorMobileNoInvalid" style="visibility:hidden;">*Please provide your first name.</span>
    </td>
    </tr>
    </table>
    <br/>
    <p>Note: All the fields marked with * are mandatory.</p><br/>
    <p><input type="checkbox" name="chkAgree" onclick="EnableSubmit(this)" /> I here by declare that the above data entered by me is true to my knowledge. </p>
    <br/>
    
    <div class="style2">
    <table>
    <tr>
    <td><button type="submit" id="submit" disabled style="width: 80px;height: 40px">Submit</button></td>
    <td><div class="divider"></div></td>
    <td><button type="reset" style="width: 80px;height: 40px">Reset</button></td>
    </tr>
    </table>
    </div>
     </form>'
    

    这是我的Java脚本代码

    function validate(){
    var valid = true;
    var validationMessage = 'Please correct the following errors:\r\n';
    var regex=[A-Z][a-zA-Z]*+$;
    var pinRegex="^[0-9]{0,6}$" ;
    var emailRegex=/^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
    var stdRegex="^[0-9]{0,6}$";
    var phoneRegex="^[0-9]{0,8}$";
    var mobileRegex="^[0-9]{0,10}$";
    
    if (document.getElementById('fname').value.length == 0) {
    validationMessage = validationMessage + '  - First name is missing\r\n';
    document.getElementById('errorFirstNameMissing').style.visibility='visible';
    valid = false;
    }
    else if(document.getElementById('fname').value!=regex){
        validationMessage = validationMessage + '  - First name is not valid can contain only letters from A-z and a-z\r\n';
        document.getElementById('errorFirstNameInValid').style.visibility='visible';
        valid = false;
        }
    else{
        document.getElementById('errorFirstNameMissing').style.visibility='hidden';
        document.getElementById('errorFirstNameInValid').style.visibility='hidden';
    }
    
    
    if (document.getElementById('lname').value.length == 0){
    validationMessage = validationMessage + '  - Last name is missing\r\n';
    document.getElementById('errorLastNameMissing').style.visibility='visible';
    valid = false;
    }
    else if(document.getElementById('lname').value!=regex){
        validationMessage = validationMessage + '  - Last name is not valid can contain only letters from A-z and a-z\r\n';
        document.getElementById('errorLastNameInValid').style.visibility='visible';
        valid = false;
        }
    else {
        document.getElementById('errorLastNameMissing').style.visibility='hidden';
        document.getElementById('errorLastNameInValid').style.visibility='hidden';
    }
    
    if (document.getElementById('gender').value == 'unknown'){
    validationMessage = validationMessage + '  - Please select a gender\r\n';
    document.getElementById('errorMissingGender').style.visibility='visible';
    valid = false;
    }
    else {
        document.getElementById('errorMissingGender').style.visibility='hidden';
        }
    
    if (document.getElementById('category').value == 'unknown'){
        validationMessage = validationMessage + '  - Please select a category\r\n';
        document.getElementById('errorMissingCategory').style.visibility='visible';
        valid = false;
        }
    else {
        document.getElementById('errorMissingCategory').style.visibility='hidden';
        }
    
    if (document.getElementById('SnapHost_Calendar').value.length == 0){
    validationMessage = validationMessage + '  - Date of Birth is missing\r\n';
    document.getElementById('errorMissingDOB').style.visibility='visible';
    valid = false;
    }
    else {
        document.getElementById('errorMissingDOB').style.visibility='hidden';
        }
    
    if (document.getElementById('addr').value.length == 0){
        validationMessage = validationMessage + '  - Address is missing\r\n';
        document.getElementById('errorMissingAddress').style.visibility='visible';
        valid = false;
        }
    else {
        document.getElementById('errorMissingAddress').style.visibility='hidden';
        }
    
    if (document.getElementById('state').value == 'Select state'||''){
        validationMessage = validationMessage + '  - Please select a state\r\n';
        document.getElementById('errorMissingState').style.visibility='visible';
        valid = false;
        }
    else {
        document.getElementById('errorMissingState').style.visibility='hidden';
        }
    
    
    if (document.getElementById('city').value == 'Select city'||''){
        validationMessage = validationMessage + '  - Please select a city\r\n';
        document.getElementById('errorMissingCity').style.visibility='visible';
        valid = false;
        }
    else {
        document.getElementById('errorMissingCity').style.visibility='hidden';
        }
    
    if (document.getElementById('pin').value.length == 0){
        validationMessage = validationMessage + '  - Pin code is missing\r\n';
        document.getElementById('errorMissingPinCode').style.visibility='visible';
        valid = false;
        }
    else if(document.getElementById('pin').value!=pinRegex){
        validationMessage = validationMessage + '  - Pin code is not valid can contain only digits from 0-6\r\n';
        document.getElementById('errorPinCodeInvalid').style.visibility='visible';
        valid = false;
        }
    else{
        document.getElementById('errorMissingPinCode').style.visibility='hidden';
        document.getElementById('errorPinCodeInvalid').style.visibility='hidden';
    }
    
    
    if (document.getElementById('email').value.length == 0){
    validationMessage = validationMessage + '  - Email is missing\r\n';
    document.getElementById('errorMissingEmail').style.visibility='visible';
    valid = false;
    }
    else if(document.getElementById('email').value!=emailRegex){
        validationMessage = validationMessage + '  - Email is not valid\r\n';
        document.getElementById('errorEmailInvalid').style.visibility='visible';
        valid = false;
        }
    else{
        document.getElementById('errorMissingEmail').style.visibility='hidden';
        document.getElementById('errorEmailInvalid').style.visibility='hidden';
    }
    
    if(document.getElementById('std').value!=stdRegex){
        validationMessage = validationMessage + '  - Pin code is not valid\r\n';
        document.getElementById('errorStdCodeInvalid').style.visibility='visible';
        valid = false;
        }
    else{
        document.getElementById('errorStdCodeInvalid').style.visibility='hidden';
        }
    
    if(document.getElementById('phone').value!=phoneRegex){
        validationMessage = validationMessage + '  - Phone Nummber is not valid can contain digits only from 0-9\r\n';
        document.getElementById('errorPhoneNoInvalid').style.visibility='visible';
    
        valid = false;
        }
    else{
        document.getElementById('errorPhoneNoInvalid').style.visibility='hidden';
        }
    
    if (document.getElementById('mobile').value.length == 0){
        validationMessage = validationMessage + '  -Mobile number is missing\r\n';
        document.getElementById('errorMissingMobileNo').style.visibility='visible';
        valid = false;
        }
    else if(document.getElementById('mobile').value!=mobileRegex){
        validationMessage = validationMessage + '  - Mobile Nummber is not valid can contain digits only from 0-9\r\n';
        document.getElementById('errorMobileNoInvalid').style.visibility='visible';
        valid = false;
        }
    else{
        document.getElementById('errorMissingMobileNo').style.visibility='hidden';
        document.getElementById('errorMobileNoInvalid').style.visibility='hidden';
    }
    
    
    if (valid == false){
    alert(validationMessage);
    }
    return valid;
    }'
    

2 个答案:

答案 0 :(得分:0)

看起来你有错字:

您已声明v位于lowercase的函数。

function validate(){

并且您以大写V

调用它
<form name="Register" action="RegisterServlet" method="post" onSubmit="return Validate()">

因此,请尝试更改您的函数名称或在表单的onSubmit事件中输入正确的函数名称。

<强>更新

尝试将以下行用于正则表达式:

else if(!regex.test(document.getElementById('fname').value)){

答案 1 :(得分:-1)

return Validate()

应该是:

return validate()