javascript中的表单验证无效

时间:2014-04-03 09:11:18

标签: javascript validation

这是一个简单的JavaScript程序,但它无法正常工作,请帮助我

<script type="text/javascript" src="validate.min.js">
        var ck_name = /^[A-Za-z0-9 ]{3,20}$/;
        var ck_email = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i 
        function validate(form){
        var name = form.txtname.value;
        var fname = form.txtfname.value;
        var country = form.txtcountry.value;
        var state = form.txtstate.value;
        var city = form.txtcity.value;
        var job = form.txtjob.value;
        var email = form.txtemail.value;
        var des = form.des.value;
        var gender = form.gender.value;


 var errors = [];

 if (!ck_name.test(name)) {
  errors[errors.length] = "You valid Name .";
 }
 if (!ck_name.test(fname)) {
  errors[errors.length] = "You valid Father's Name .";
 }
 if (!ck_name.test(country)) {
  errors[errors.length] = " invalid country .";
 }
  if (!ck_name.test(state)) {
  errors[errors.length] = "You valid state .";
 }
 if (!ck_name.test(city)) {
  errors[errors.length] = "You valid city .";
 }

 if (!ck_email.test(email)) {
  errors[errors.length] = "You must enter a valid email address.";
 }

 if (des==-1) {
  errors[errors.length] = "Select Designation";
 }


 if (errors.length > 0) {
  reportErrors(errors);
  return false;
 }

 return true;
}

function reportErrors(errors){
 var msg = "Please Enter Valide Data...\n";
 for (var i = 0; i<errors.length; i++) {
  var numError = i + 1;
  msg += "\n" + numError + ". " + errors[i];
 }
 alert(msg);
}
</script>

    </script>
//html part
<form method="post" action="#" enctype="multipart/form-data" " name="form" onSubmit="return validate(this);">
        <table>
            <tr>
                <td><label for="txtname">Name</label></td>
                <td><input type="text" name="txtname" id="txtname"          </tr>
            <tr>
                <td><label for="txtfname">Father Name</label></td>
                <td><input type="text" name="txtfname" id="txtfname" /></td>
            </tr>
            <tr>
                <td><label>Gender</label></td>
                <td>Male<input type="radio" name="gender" checked="checked" value="m"  class="rd" />
                    Female<input type="radio" name="gender" value="f" class="rd" />
                </td>
            </tr>
            <tr></tr>
            <tr>
            <td>
                <input onchange="ValidateFileUpload(this);" type="file"  name="file"  id="filename"/></td>
                 <td><img alt="Image Display Here" id="test" src="./upload/icon3.jpg" height="100px" width="100px"  /></td>
                 <?php
                 if(isset($_FILES["file"]["name"]))
                 ?>
            </tr>

            <tr>
                <td><label>Designation</label></td>
                <td><select name="des" class="rg11" id="des">
                    <option value="-1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-Select Designation-</option>
                    <option value="Employed">Employed</option>
                    <option value="selfemployed">Self-Employed</option>
                    <option value="retired">Retired</option>          
                        </select></td>
            </tr>
            <tr>
                <td>
                    <label>Title of Job</label></td>
                    <td><input type="text" name="txtjob" id="txtjob"   /></td>
            </tr>

            <tr>
                <td>
                    <label>Country</label></td>
                    <td><input type="text" name="txtcountry" id="txtcountry"  /></td>
            </tr>
            <tr>
                <td>
                    <label>State</label></td>
                    <td><input type="text" name="txtstate" id="txtstate"  /></td>
            </tr>
            <tr>
                <td>
                    <label>City</label></td>
                    <td><input type="text" name="txtcity" id="txtcity" /></td>
            </tr>
            <tr>
                <td>
                    <label>Contact no</label></td>
                    <td><input type="tel" name="txtcontact" value="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Enter your contact no" class="rg11" onfocus="if(this.value=='&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Enter your contact no'){this.value='';}"  />
                        &nbsp;&nbsp;Private<input type="radio" name="contactst" value="0" />&nbsp;&nbsp;Public<input type="radio" name="contactst" checked="checked"  value="1"/>
                    </td>
            </tr>
            <tr>
                <td>
                    <label>Email</label></td>
                    <td><input type="email" name="txtemail" id="txtemail"  />
                        &nbsp;&nbsp;Private<input type="radio" name="emailst" value="0" />&nbsp;&nbsp;Public<input type="radio" name="emailst" checked="checked" value="1" />
                    </td>
            </tr>
        </table>
        <input type="submit" name="btnsave" value="submit" class="tb20"  /></td>

     </form>

3 个答案:

答案 0 :(得分:1)

脚本元素从 其中的文本节点或src属性的URL获取其脚本。不是两个。

如果要加载两个脚本(一个来自URL并在文本节点中打开),那么您需要两个脚本元素。

答案 1 :(得分:0)

关闭你的js脚本标签,这会导致错误(请注意包括 jquery 文件)

<script type="text/javascript" src="validate.min.js"></script> 
                                                 <!-- ^^^^^CLOSE your SCRIPT tag -->

<script type="text/javascript"> <!--START a new JS your SCRIPT tag -->

 var ck_name = /^[A-Za-z0-9 ]{3,20}$/;
        var ck_email = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i 
        function validate(form){
        var name = form.txtname.value;
        var fname = form.txtfname.value;
        var country = form.txtcountry.value;
        var state = form.txtstate.value;
        var city = form.txtcity.value;

答案 2 :(得分:0)

请尝试使用以下Java脚本代码。如果有任何疑虑,请告诉我。

<script type="text/javascript" src="validate.min.js" > </script>
<script type="text/javascript">
   var ck_name = /^[A-Za-z0-9 ]{3,20}$/;
   var ck_email = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/;

   function validate(form){
   var name = form.txtname.value;
   var fname = form.txtfname.value;
   var country = form.txtcountry.value;
   var state = form.txtstate.value;
   var city = form.txtcity.value;
   var job = form.txtjob.value;
   var email = form.txtemail.value;
   var des = form.des.value;
   var gender = form.gender.value;


   var errors = [];

   if (!ck_name.test(name)) {
   errors[errors.length] = "You valid Name .";
   }
   if (!ck_name.test(fname)) {
   errors[errors.length] = "You valid Father's Name .";
   }
   if (!ck_name.test(country)) {
   errors[errors.length] = " invalid country .";
   }
   if (!ck_name.test(state)) {
   errors[errors.length] = "You valid state .";
   }
   if (!ck_name.test(city)) {
   errors[errors.length] = "You valid city .";
   }

   if (!ck_email.test(email)) {
   errors[errors.length] = "You must enter a valid email address.";
   }

   if (des==-1) {
   errors[errors.length] = "Select Designation";
   }


   if (errors.length > 0) {
   reportErrors(errors);
   return false;
   }

   return true;
   }

   function reportErrors(errors){
   var msg = "Please Enter Valide Data...\n";
   for (var i = 0; i<errors.length; i++) {
   var numError = i + 1;
   msg += "\n" + numError + ". " + errors[i];
   }
   alert(msg);
   }
</script>