Serialise HTML Form with time/text input type and validation of time fields

I am trying to create an HTML Form that will be used to send data to the backend in C#.

Below is the Form which consists of 2 fields: FROMTIME and TOTIME.

I need to make sure that the user inputs the values in the format of HH:MM but this is not feasible without validation.

I have tried using input type="time" but this does not allow me to serialise the data and send that to the backend.

I am using $("#editForm").serialize or $("#editForm").serializeObject or $("#editForm").serializeArray but the data is empty because the serialisation does not include the Time data.

Any ideas please?

Is there a way to force the user to input hour and minutes in the format of HH:MM?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><form id="editForm">
<table>
<tr>
<th>From Time: </th>
<td style="background-color:white;">
<input type="text" id="FROMTIME" placeholder="HH:MM" name="FROMTIME" value="@Model.FROMTIME" asp-for="FROMTIME" required form="editForm" />
</td>
</tr>
<tr>
<th>To Time: </th>
<td style="background-color:white;">
<input type="text" id="TOTIME" placeholder="HH:MM" name="TOTIME" value="@Model.TOTIME" asp-for="TOTIME" required form="editForm" />
</td>
</tr>
</table>
</form>
</code>
<code><form id="editForm"> <table> <tr> <th>From Time: </th> <td style="background-color:white;"> <input type="text" id="FROMTIME" placeholder="HH:MM" name="FROMTIME" value="@Model.FROMTIME" asp-for="FROMTIME" required form="editForm" /> </td> </tr> <tr> <th>To Time: </th> <td style="background-color:white;"> <input type="text" id="TOTIME" placeholder="HH:MM" name="TOTIME" value="@Model.TOTIME" asp-for="TOTIME" required form="editForm" /> </td> </tr> </table> </form> </code>
<form id="editForm">
  <table>
    <tr>
      <th>From Time: </th>
      <td style="background-color:white;">       
        <input  type="text" id="FROMTIME"  placeholder="HH:MM" name="FROMTIME" value="@Model.FROMTIME" asp-for="FROMTIME" required form="editForm" />
      </td>
    </tr>
    <tr>
      <th>To Time: </th>
      <td style="background-color:white;">
        <input type="text" id="TOTIME"  placeholder="HH:MM" name="TOTIME" value="@Model.TOTIME" asp-for="TOTIME" required form="editForm" />
      </td>
    </tr>
  </table>
</form>

The Ajax call for the backend using serialisation for the data are the following lines of code. The printout at the console is empty because the Time input types are not picked up by jQuery:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$("#editForm").on("submit", function(event) {
console.log($("editForm").serializeObject());
$.ajax({
url: "/Items/SaveItem/",
type: "POST",
data: $("#editForm").serializeObject(),
success: function(data) {
if (data.message == "OK") {
success("Item has been successfully saved!");
}
},
});
});
</code>
<code>$("#editForm").on("submit", function(event) { console.log($("editForm").serializeObject()); $.ajax({ url: "/Items/SaveItem/", type: "POST", data: $("#editForm").serializeObject(), success: function(data) { if (data.message == "OK") { success("Item has been successfully saved!"); } }, }); }); </code>
$("#editForm").on("submit", function(event) {

    console.log($("editForm").serializeObject());
    $.ajax({
        url: "/Items/SaveItem/",
        type: "POST",
        data: $("#editForm").serializeObject(),
        success: function(data) {
            
            if (data.message == "OK") {
                success("Item has been successfully saved!");
                }
        },
        
    });

});

4

You might try my codes. You have asp-for taghelper so that I create an asp.net-core MVC application for test. Here’s my controller and model.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public IActionResult SubmitDate() {
var model = new MyTime { FromTime=DateTime.Now, ToTime=DateTime.Now.AddHours(3)};
return View(model);
}
[HttpPost]
public string PostTime(MyTime myTime) {
return "success";
}
public class MyTime {
public DateTime FromTime { get; set; }
public DateTime ToTime { get; set; }
}
</code>
<code>public IActionResult SubmitDate() { var model = new MyTime { FromTime=DateTime.Now, ToTime=DateTime.Now.AddHours(3)}; return View(model); } [HttpPost] public string PostTime(MyTime myTime) { return "success"; } public class MyTime { public DateTime FromTime { get; set; } public DateTime ToTime { get; set; } } </code>
public IActionResult SubmitDate() {
    var model = new MyTime { FromTime=DateTime.Now, ToTime=DateTime.Now.AddHours(3)};
    return View(model); 
}

[HttpPost]
public string PostTime(MyTime myTime) { 
    return "success";
}

public class MyTime {
    public DateTime FromTime { get; set; }
    public DateTime ToTime { get; set; }
}

And here’s my view.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@model WebAppMvc.Controllers.MyTime;
<form id="editForm" asp-action="PostTime" asp-controller="Form">
<table>
<tr>
<th>From Time: </th>
<td style="background-color:white;">
<input type="time" placeholder="HH:MM" name="FROMTIME" value="@Model.FromTime" asp-for="FromTime" required form="editForm" />
</td>
</tr>
<tr>
<th>To Time: </th>
<td style="background-color:white;">
<input type="time" placeholder="HH:MM" name="TOTIME" value="@Model.ToTime" asp-for="ToTime" required form="editForm" />
</td>
</tr>
</table>
<button type="submit">
Submit
</button>
</form>
@section Scripts{
<script>
$("#editForm").on("submit", function (e) {
console.log($("#editForm").serialize());
alert(1);
e.preventDefault();
$.ajax({
url: "/Form/PostTime/",
type: "POST",
data: $("#editForm").serialize(),
success: function (data) {
alert(data);
},
});
});
</script>
}
</code>
<code>@model WebAppMvc.Controllers.MyTime; <form id="editForm" asp-action="PostTime" asp-controller="Form"> <table> <tr> <th>From Time: </th> <td style="background-color:white;"> <input type="time" placeholder="HH:MM" name="FROMTIME" value="@Model.FromTime" asp-for="FromTime" required form="editForm" /> </td> </tr> <tr> <th>To Time: </th> <td style="background-color:white;"> <input type="time" placeholder="HH:MM" name="TOTIME" value="@Model.ToTime" asp-for="ToTime" required form="editForm" /> </td> </tr> </table> <button type="submit"> Submit </button> </form> @section Scripts{ <script> $("#editForm").on("submit", function (e) { console.log($("#editForm").serialize()); alert(1); e.preventDefault(); $.ajax({ url: "/Form/PostTime/", type: "POST", data: $("#editForm").serialize(), success: function (data) { alert(data); }, }); }); </script> } </code>
@model WebAppMvc.Controllers.MyTime;

<form id="editForm" asp-action="PostTime" asp-controller="Form">
    <table>
        <tr>
            <th>From Time: </th>
            <td style="background-color:white;">
                <input type="time" placeholder="HH:MM" name="FROMTIME" value="@Model.FromTime" asp-for="FromTime" required form="editForm" />
            </td>
        </tr>
        <tr>
            <th>To Time: </th>
            <td style="background-color:white;">
                <input type="time" placeholder="HH:MM" name="TOTIME" value="@Model.ToTime" asp-for="ToTime" required form="editForm" />
            </td>
        </tr>
    </table>
    <button type="submit">
        Submit
    </button>
</form>

@section Scripts{
    <script>
        $("#editForm").on("submit", function (e) {
            console.log($("#editForm").serialize());
            alert(1);
            e.preventDefault();
            $.ajax({
                url: "/Form/PostTime/",
                type: "POST",
                data: $("#editForm").serialize(),
                success: function (data) {
                    alert(data);
                },
            });
        });
    </script>
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật