为什么我的表单验证工作不正确?

时间:2017-04-21 01:11:05

标签: javascript html forms validation

我正在为一个项目创建一个表单,我必须设置一个验证规则,这样如果没有为该名称输入任何内容,则会出现一个警告,告诉用户“必须填写名称”我知道这可能是一个愚蠢的菜鸟错误,但我不太清楚为什么它不工作,因为我是新的JavaScript。

我没有将CSS作为不必要的内容。我也没有把信息发布到任何东西。

 <body>
 <div class="form_settings">
          <form name="MyForm"  onSubmit="return validateForm()" 
 method="post">
            <p><span>Name</span><input class="contact" type="text" 
  name="fname" value="" /></p>
            <p><span>Email Address</span><input class="contact" type="text" 
  name="your_email" value="" /></p>
            <p><span>Re type Email Address</span><input class="contact" 
 type="text" name="your_email" value="" /></p>
            <p><span>Message</span><textarea class="contact textarea" 
 rows="8" cols="50" name="your_enquiry"></textarea></p>
            <p style="padding-top: 15px"><span>&nbsp;</span><input 
class="submit" type="submit" name="contact_submitted" value="Submit" /></p>
          </form>

 </body>
   
 public String[] getModulesAtCurrentSlot(int timeslotInt, String moduleID, String Day)
        {
            List<String> modulesList = new List<string>();
            if (conn.State.ToString() == "Closed")
            {
                conn.Open();
            }
            SqlCommand newCmd = conn.CreateCommand();
            newCmd.Connection = conn;
            newCmd.CommandType = CommandType.Text;
            newCmd.CommandText = "SELECT DISTINCT Module.ModuleCode,ClassType.ClassTypeName,Convert(time,Class.StartTime), Convert(time,Class.EndTime),Building.BuildingName,RoomCode.RoomCode,Class.Color" +
                     " FROM Class INNER JOIN Module ON Class.ModuleId = Module.ModuleId INNER JOIN RoomCode ON Class.RoomCodeId = RoomCode.RoomcodeId INNER JOIN Building ON RoomCode.BuildingId = Building.BuildingId INNER JOIN Days ON Class.DayId = Days.DayID INNER JOIN ClassType ON Class.ClassTypeId = ClassType.ClassTypeId WHERE " +
                     " Module.ModuleId = " + moduleID + " AND Convert(Date,StartTime) = '" + Day + "' AND " + timeslotInt.ToString() + " BETWEEN ClassScheduleStartTimeId and ClassScheduleEndTimeId";
            SqlDataReader dr = newCmd.ExecuteReader();
            while (dr.Read())
            {
                String current = "<div class='slot' " + (!dr.IsDBNull(6) ? "style=\"background-color: " + dr.GetString(6) + ";\"" : "") + ">";
                current += "<div class='line1'>" + dr.GetString(0) + "&nbsp;" + dr.GetString(1) + "</div>";// +"<br />";
                current += "<div class='line2'>" + dr.GetTimeSpan(2).ToString().TrimEnd('0').TrimEnd('0').TrimEnd(':') + "&nbsp;-&nbsp;" + dr.GetTimeSpan(3).ToString().TrimEnd('0').TrimEnd('0').TrimEnd(':') + "</div>";// +"<br />";
                current += "<div class='line3'>" + dr.GetString(4) + "&nbsp;" + dr.GetString(5) + "</div>";
                current += "</div>";
                modulesList.Add(current);
            }
            conn.Close();
            return modulesList.ToArray();
        }

1 个答案:

答案 0 :(得分:0)

试试这个:

function validateForm() {
    var x = document.getElementsByClassName("contact")[0].value;
    if (x == "") {
        alert("Name must be filled out");
    }

    return false;
}
<form name="MyForm" onsubmit="validateForm();">
        <p><span>Name</span>
            <input class="contact" type="text" name="fname" value="" />
        </p>
        <p><span>Name</span><input class="contact" type="text" name="fname" value="" /></p>
        <p><span>Email Address</span><input class="contact" type="text" name="your_email" value="" /></p>

        <p><span>Re type Email Address</span><input class="contact" type="text" name="your_email" value="" /></p>

        <p><span>Message</span><textarea class="contact textarea" rows="8" cols="50" name="your_enquiry"></textarea></p>

        <p style="padding-top: 15px"><span>&nbsp;</span>
            <input class="submit" type="submit" value="submit" />
        </p>
    </form>