1

我在一个网站上工作,我必须检查一些表格,这样任何字段都不应该为空,如果有任何空字段,它应该写这个字段在字段名称的右侧是空的

我的表单代码是:-

        <form action="Owners Infoback.php"  onsubmit="return validateForm()"  method="post" name="enquiry" id="" class="form-body-02">
    <ul>
        <li>1. Name of owner <input name="Name_of_owner" type="text" class="textarea-bk-02" id=""  value="" style="border:none; width:330px; margin-left:40px; margin-top:15px; height:20px;"/>

        </li>
        <li>
         2. Name of company:<br />
         <p>(Enter name registered)</p></label>
        <input name="Name_of_company_registered" type="text" class="textarea-bk-02" id=""  value="" style="border:none; width:330px; margin-left:40px; margin-top:13px; height:20px;"/>
        </li>
        <li>
          3. Address:<p>(Write your own manufacturer address)</p>

        <input name="Owner_address" type="text" class="textarea-bk-02" id=""  value="" style="border:none; width:330px; height:20px; margin-left:40px; margin-top:13px;"/></li>

        </li>
        <li>
          4. Email id:
          <input name="Owner_Email_id" type="text" class="textarea-bk-02" id=""  value="<?php echo "{$row[3]}";?>" style="border:none; width:330px; margin-left:40px; margin-top:13px; height:20px;"/>
        </li>
        <li><input name="Save" type="submit"  class="send-btns-02 right" value="save" style="margin-top:5px;" >
        <div class="clear"></div>
        </li>
    </ul>

我的java脚本函数是:-

   function validateForm(string msz)
  {
 var x=document.forms["enquiry"]["Name_of_owner"].value;
if (x==null || x=="")
{
msz="First name must be filled out");
return msz;
}
x=document.forms["enquiry"]["Name_of_company_registered"].value;
if (x==null || x=="")
{ 
alert("Name of company registered filled out");
return false;
}
x=document.forms["enquiry"]["Owner_address"].value;
if (x==null || x=="")
{
alert("Owner addressmust be filled out");
return false;
}
 x=document.forms["enquiry"]["Owner_Email_id"].value;
 var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
 alert("Not a valid e-mail address");
 return false;
}
}

我想要而不是警报功能,它应该返回一个星号(*)和消息请填写此表格,就在字段名称的右边,请帮助我..提前谢谢..

4

2 回答 2

1

You could put hidden Divs next to each form input:

<div id="nameOfOwnerError" style="display:none">*</div>

And do this for each input, then you can choose to show/hide them depending whether they are valid or not:

$("#nameOfOwnerError").show();

Edit: Neeraj - try this.

In Javascript (sorry), something along the lines of.....

 var nameOfOwnerError = document.getElementById("nameOfOwnerError");
nameOfOwnerError.style.display="visible";

Off the top of my head, but you get the idea. Main point was simply to hide the asterisks and show hide them using the code you have.

Edit2: Sorry Neeraj, was untested - this works for me:

document.getElementById('nameOfOwnerError').style.display = "";

makes the element reappear and you could do

...display="none"; 

in the js code to hide again

Edit3: You could add an OnKeyUp event to the textboxes, so that when a text is entered into the textbox, you could trigger you validation script:

<input name="Name_of_owner" type="text" onkeyup="validateForm('');" class="textarea-bk-02" id=""  value="" style="border:none; width:330px; margin-left:40px; margin-top:15px; height:20px;"/>

Using the ifs to determine if the field is empty (as you are doing), you should be able to show/hide the asterisks as soon as a user has typed into a given textbox.

Got to go for now but something along these lines is what you're looking for, good luck!

于 2012-06-01T09:04:30.173 回答
0

你不希望它返回一个 *. 您希望它返回 false (就像它正在做的那样)。您还想要在该字段旁边画一个 * 以及您希望他们做什么的消息。

For this to happen you will either want to add an invisible div next to the form field and show it (whichever one it is, based on the missing data) when the user tries to submit an invalid form. Or you can add the element at run time using document.createElement and attach it to the parent of the form field. In either case you'll have to make sure your HTML accommodates the extra element.

于 2012-06-01T09:03:55.297 回答