Pau Gacusan
Pau Gacusan

Reputation: 67

Numbers and Slash (Dates) only textbox asp.net

I have a gridview that contains names and dates. I also placed some textbox on the header of the gridview that can be used for searching. I want my textbox for searching dates to restrict user from typing other characters than numbers and slash. So basically, I want a textbox that only allows users to type dates. It should allow the user to search by typing Month(01-12), Month and Day(01/13,12/30) or Month, Day, and Year(01/13/2018, 12/30/2018)...

I used TextMode: Date on the textbox before but it requires the user to complete the full mm/dd/yyyy format which restricts users to search with just month or month and day only. I tried some javascripts I found in the internet but it doesn't meet my requirements. Any help would be appreciated.

Upvotes: 2

Views: 765

Answers (3)

vivek ramasamy
vivek ramasamy

Reputation: 115

Kindly Use the below javascript which answers your Question

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<style type="text/css">
        body
        {
          font-size: 9pt;
          font-family: Arial;
        }
    </style>
</head>
<body>
Enter Date:
  <input type="text" id="text1" onkeypress="return IsValidData(event);" 
 ondrop="return false;"
    onpaste="return false;" />
<span id="error" style="color: Red; display: none">* Invalid Character</span>
<script type="text/javascript">
    var specialKeys = new Array();
    specialKeys.push(8); //Backspace
    specialKeys.push(9); //Tab
    specialKeys.push(46); //Delete
    specialKeys.push(36); //Home
    specialKeys.push(35); //End
    specialKeys.push(37); //Left
    specialKeys.push(39); //Right
    function IsValidData(e) {
        var keyCode = e.keyCode == 0 ? e.charCode : e.keyCode;
        var ret = ((keyCode >= 48 && keyCode <= 57) || (keyCode == 43) || (keyCode == 47) || (specialKeys.indexOf(e.keyCode) != -1 && e.charCode != e.keyCode));
        document.getElementById("error").style.display = ret ? "none" : "inline";
        return ret;
    }
</script>

If this POST answers your Question, please check the green tick.

Upvotes: 1

Gustav
Gustav

Reputation: 56026

You could use Parse which is quite forgiving:

string s = "5/12";
DateTime d = DateTime.Parse(s);

// d will hold: 2018-12-05

Of course, wrap it in a try-catch.

Upvotes: 1

Koby Douek
Koby Douek

Reputation: 16693

You can use the asp:RegularExpressionValidator to validate a TextBox's Text property.

In the example below, I used the ^\d{4}\/\d{2}\/\d{4}$ regular expression, which allows only positive numbers with slashes, as you required.

<asp:TextBox ID="txt1" runat="server"/>
<asp:RegularExpressionValidator ID="myRegex" 
                                runat="server"     
                                ErrorMessage="Invalid characters." 
                                ControlToValidate="txt1"     
                                ValidationExpression="^\d{4}\/\d{2}\/\d{4}$" />

Upvotes: 0

Related Questions