1

<head>
<title> Add User page</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<form name='f2' action="insert_ac.php" method="post" >
<script src="validation.js" type="javascript/text"></script> 
</head>
<body  onload="firstfocus();">
<table align="center" border="0" cellpadding="3" cellspacing="1">
 <tr>
 <td> First Name</td><td > : </td>
 <td>  <input type='text' name='fname' id='fid' size="50" style="background-color:#abcddd; height:18px;" value='' maxlength="100" onblur="fname_validation(5,12);">    </td>
  </tr>
 <tr>
<td> Last Name</td><td> : </td>
 <td> <input type='text' name='lname' id='lid' size="50" style="background-color:#abcddd; height:18px; "value='' maxlength="100" onblur="lname_validation(5,12);"> </td>
  </tr>
 <tr>
<td> Gender</td><td> : </td>
<td> <input type='radio' name='gend' id='m' value='M' checked>Male <input type='radio' name='gend' id='f' value='F'>Female</td>
</tr>
<tr>
<td> Phone Number</td><td> : </td>
 <td> <input type='number_format' name='phone' id='phno'size="50" style="background-color:#abcddd; height:18px; " value=''onblur="allnumeric();"></td>
 </tr>
  <tr>
  <td> Work Experiance</td><td> : </td>
   <td><select name="exp" onblur="expselect();">
   <option > Select One</option>
   <option selected="" value="Default"> Select One </option>
   <option value="F"> Fresher </option>
   <option value="E"> Experiance</option>
   </select></td>
   </tr>
   <tr>
   <td>User Name</td><td>:</td><td> <input type='txt' name='uname' id='uid'size="50" style="background-color:#abcddd; height:18px; " value=''onblur="userid_validation(5,10);">   </td>
   </tr>
    <tr>
   <td>Password</td><td>:</td><td> <input type='password' name='pwd' id='pid' size="50" style="background-color:#abcddd; height:18px; " value=''onblur="passid_validation(7,12);"></td>
   </tr>
  <tr>
 <td>&nbsp;</td>
  <td>&nbsp;</td>
 <td width="84"><input name="enter" class="btn_login" type="submit" value="Submit"  onsubmit="alert('Data stored successfully');"/>&nbsp;&nbsp;<input name="cancle" class="btn_login" type="reset" value="Cancle" /></td>
  </tr>
 </table>

 </form>
 </body>
 </html>

我使用脚本标记包含了外部 javascript 文件,但验证不起作用。

这是我保存为validation.js 的外部javascript 代码,我无法在其中找到错误,请帮助我。

// After form loads focus will go to first name field.  
function firstfocus()  
{  
var fname = document.f2.fname.focus();  
return true;  
}  
// This function will validate First name  
function fname_validation(mx,my)  
{  
var fname = document.f2.fname;  
var fname_len = fname.value.length; 
var letters = /^[A-Za-z]+$/;  
 if (fname_len == 0 || fname_len >= my || fname_len < mx)  
{  
alert("First Name should not be empty / length be between "+mx+" to "+my);  
fname.focus();  
return false;  
      if(fname.value.match(letters)) 
    {  
// Focus goes to next field i.e.Last Name  
 document.f2.lname.focus();  
return true;  
 }  
}  
 }

 // This function will validate Last name  
function lname_validation(mx,my)  
 {  
 var lname = document.f2.lname;  
 var lname_len = lname.value.length; 
 var letters = /^[A-Za-z]+$/;    
 if (lname_len == 0 || lname_len >= my || lname_len < mx)  
 {  
  alert("Last Name should not be empty / length be between "+mx+" to "+my);  
  lname.focus();  
    return false;  
    if(fname.value.match(letters)) 
    {  
    // Focus goes to next field i.e.Phone Number  
     document.f2.phone.focus();  
   return true;  
   }  
   }  
   }

  // This function will validate Phone Number.  
  function allnumeric()  
   {   
   var phone = document.f2.phone;  
     var numbers = /^[0-9]+$/;  
     if(phone.value.match(numbers))  
     {  
    // Focus goes to next field i.e. Experiance.  
   document.f2.exp.focus();  
    return true;  
   }  
   else  
   {  
   alert('Phone Number must have numeric characters only');  
   phone.focus();  
   return false;  
   }  
   }    
   // This function will select Experiance.  
    function expselect()  
    {  
   var exp = document.f2.exp;  
   if(exp.value == "Default")  
   {  
   alert('Select your Experiance from the list');  
   exp.focus();  
   return false;  
   }  
   else  
   {  
   // Focus goes to next field i.e. Username Code.  
   document.f2.uname.focus();  
   return true;  
  }  
  }  

  // This function will validate User Name.  
  function allLetter()  
   {   
   var uname = document.f2.uname;  
   var letters = /^[A-Za-z]+$/;  
  if(uname.value.match(letters))  
   {  
   // Focus goes to next field i.e. Password.  
    document.f2.pwd.focus();  
     return true;  
    }  
    else  
     {  
     alert('Username must have alphabet characters only');  
    uname.focus();  
     return false;  
     }  
     }  

    // This function will validate Password.  
   function passid_validation(mx,my)  
   {  
   var passid = document.registration.passid;  
    var passid_len = passid.value.length;  
   if (passid_len == 0 ||passid_len >= my || passid_len < mx)  
    {  
    alert("Password should not be empty / length be between "+mx+" to "+my);  
     passid.focus();  
     return false;  
     }  
     }  
4

3 回答 3

1

如果 .js 与 html 不在同一个文件夹中,则必须引用它

编辑 function fname_validation(mx,my)
{

缺少右括号!

于 2013-04-26T10:17:24.520 回答
1

尝试更改<script type="javascript/text"><script type="text/javascript"> 仔细检查您的 js 文件的引用。另外,进行警报检查以确定它是否真的通过了 firstFocus() 函数。

于 2013-04-26T10:22:39.173 回答
1

我更改了 java 脚本文件,现在我可以在我的表单中找到验证...

谢谢你的帮助..

我在这里分享我的代码...

HTML 代码

   <form name='f1' action="insert_ac.php" method="post" onSubmit="return validateForm();" >
     <tr>
   <td height="25" height="25" colspan="2" bgcolor="#EC6921" class="form_heading">Add User Form</td>
</tr>
                     <tr>
                    <td bgcolor="#FFFFFF"   height="28" class="form_txt"> First Name:</td> 
                    <td bgcolor="#FFFFFF">  <input type='text' name='fname' id='fid' size="50" style="height:18px;" value='' maxlength="100" ></td>
                </tr>
                <tr>
                    <td bgcolor="#FFFFFF" height="28" class="form_txt"> Last Name:</td>
                    <td bgcolor="#FFFFFF" > <input type='text' name='lname' id='lid' size="50" style=" height:18px; "value='' maxlength="100" ></td>
                </tr>
                <tr>
                    <td bgcolor="#FFFFFF" height="28" class="form_txt"> Gender:</td>
                    <td bgcolor="#FFFFFF" > <input type='radio' name='gend' id='m' value='M' checked>Male <input type='radio' name='gend' id='f' value='F'>Female</td>
                </tr>
                <tr>
                    <td bgcolor="#FFFFFF" height="28" class="form_txt"> Phone Number:</td>
                    <td bgcolor="#FFFFFF" > <input type='number_format' name='phone' id='phno'size="50" style=" height:18px; " value=''></td>
                </tr>
                <tr>
                    <td bgcolor="#FFFFFF" height="28" class="form_txt"> Work Experiance:</td>
                    <td bgcolor="#FFFFFF" ><select name="exp" >
                            <option selected="" value="Default"> Select One </option>
                            <option value="Fresher"> Fresher </option>
                            <option value="Experiance"> Experiance</option>
                        </select></td>
                </tr>
                <tr>
                    <td bgcolor="#FFFFFF" height="28" class="form_txt">User Name:</td><td bgcolor="#FFFFFF"> <input type='txt' name='uname' id='uid'size="50" style=" height:18px; " value=''></td>
                </tr>
                <tr>
                    <td bgcolor="#FFFFFF" height="28" class="form_txt">Password:</td><td bgcolor="#FFFFFF"> <input type='password' name='pwd' id='pid' size="50" style=" height:18px; " value=''></td>
                </tr>
                     </table>
        <table align="center">
                <tr>
                  <td ><input name="enter" class="btn_login" type="submit" value="Submit" align="center"><input name="cancle" class="btn_login" type="reset" value="Cancle" valign="right" /></td>
                </tr>
   </table>
        </div>
      </div>
    </div>
   </form>


   </body>
  </html> 

这是我的java脚本文件,它工作正常......

   var RE = /^.+@.+\..{3}$/;
   var RE1 = /^[a-zA-Z]+$/;
    var RE2 = /^[0-9]{10}$/;
     var RE3 = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;
    function validateForm()
    {
      if (document.f1.fname.value == "")
      {
    window.alert("first name should NOT BE empty");
    document.f1.fname.focus();
    return false;
   }
   else
    if (RE1.test(document.f1.fname.value) == false)
    {
      alert("Invalid first name\n\
      ");
       return false;
     }
     else
    if (document.f1.fname.value.length < 3)
     {
    window.alert("Firstname must have atleast Three characters");
    document.f1.fname.focus();
    return false;
    }
     else
     if (document.f1.lname.value == "")
    {
    window.alert("Lastname should not be empty");
    document.f1.lname.focus();
    return false;
     }
    else
   if (RE1.test(document.f1.lname.value) == false)
    {
    alert("Invalid last name");
    return false;
    }
    else
     if (document.f1.lname.value.length < 4)
     {
    window.alert("Lastname must have atleast four characters");
    document.f1.lname.focus();
    return false;
     }
     else
     if (document.f1.lname.value == "")
    {
    window.alert("Last name should not be empty");
    document.f1.lname.focus();
    return false;
      }
    else
     if (document.f1.phone.value == "")
     {
    window.alert("phne no should NOT BE empty");
    document.f1.phone.focus();
    return false;
     } else
     if (RE3.test(document.f1.phone.value) == false)
     {
    alert("Invalid phone number");
    document.f1.phone.focus();
    return false;
     }
     else
     if (document.f1.exp.selectedIndex == 0)
     {
        window.alert("please select work experiance ");
       return false;
     }
     else
      if (document.f1.uname.value == "")
    {
    window.alert("UserName should not be empty");
    document.f1.uname.focus();
    return false;
    }
    else
    if (RE1.test(document.f1.uname.value) == false)
    {
    window.alert("Invalid userName ");
    document.f1.uname.focus();
    return false;
    }

    else
    if (document.f1.pwd.value == "")
    {
    window.alert("password should not be empty");
    document.f1.pwd.focus();
    return false;
     }
    else
   if (document.f1.pwd.value.length < 6)
    {
    window.alert("password must have atleast six characters");
    document.f1.pwd.focus();
    return false;
     }

    else
    {
    window.alert("User has been added successfully.");
    return true;
     }
     }
于 2013-05-09T09:35:57.827 回答