Semil Sebastian
Semil Sebastian

Reputation: 111

How to set validation for HH:MM time format?

I am doing an updation. So I have set value from database to a text box in time format like HH:MM

 00:00
 if I enter 8:00//it should show error
 if I enter 08:0//it should show error

How can I perform this???. Entering this value in a text box. Ajax code also acceptable. it is a web application form and it is in a 24 hours format also.

Upvotes: 0

Views: 7420

Answers (3)

Chandan Rajbhar
Chandan Rajbhar

Reputation: 1

Dr = cmd.ExecuteReader();
if (Dr.Read())
{
    txtReguID.Text = Dr["Registration_ID"].ToString();
    string addmissiondate = Dr["AdmissionDate"].ToString();

    txtAdmissionDate.Text = Convert.ToDateTime(addmissiondate).ToString("MM/dd/yyyy");
}

Upvotes: 0

Kavindu Dodanduwa
Kavindu Dodanduwa

Reputation: 13069

You can use DateTime.TryParseExact method

String a = "08:0"; // text as string 

DateTime time= new DateTime(); // Passed result if succeed 

 if (DateTime.TryParseExact(a, "hh:mm", new CultureInfo("en-US"), DateTimeStyles.None, out time)) {
    Console.WriteLine("pass");
 }
 else {
    Console.WriteLine("fail");
 }

Note : hh for 12 hour format, use HH for 24 hour format

Upvotes: 1

Mahesh
Mahesh

Reputation: 8892

You can use the RegularExpressionValidator and below ValidationExpression.

  ValidationExpression="^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]\040(AM|am|PM|pm)$"

Here is more info on how to use the RegularExpressionValidator.

Upvotes: 1

Related Questions