This is a migrated thread and some comments may be shown as answers.

enter key with invalid date

2 Answers 66 Views
Calendar
This is a migrated thread and some comments may be shown as answers.
newbie
Top achievements
Rank 1
newbie asked on 04 Feb 2010, 12:31 AM
I have a routine to handle enter key as save. However if the user enters an invalid date and hits enter i do not want the save functionality to be implemented. My Page.IsValid is returned true if the date entered is an invalid date.

is there a workaround for this?
below is the code for my enter key routine:

function

 

HandleEnterAsSave(sender, eventArgs) {

 

 

if (eventArgs.get_domEvent().keyCode == 13) {

 

 

//__doPostBack("btnSave", "Save");

 

 

var saveButton = document.getElementById('<%= btnSave.ClientID %>');

 

saveButton.click();

}

}

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 04 Feb 2010, 07:47 AM
Hello,

You can check whether the typed date is valid or not. And then invoke the postback only if the typed value is valid date. Here is an example that I tried.

JavaScript:
 
    function OnKeyPress(sender, eventArgs) { 
        if (eventArgs.get_keyCode() == 13) { 
            var validformat = /^\d{2}\/\d{2}\/\d{4}$/ //Basic check for format validity 
 
            if (validformat.test((sender.get_textBoxValue()))) { // You can use your own logic insted for checking date 
                var saveButton = document.getElementById('<%= btnSave.ClientID %>'); 
                saveButton.click(); 
            } 
            eventArgs.set_cancel(true); 
        } 
    } 
[Attach OnKeyPress event to DatePicker-DateInput]

-Shinu.
0
newbie
Top achievements
Rank 1
answered on 04 Feb 2010, 05:57 PM
This solution does not work for me.
My save button click is always fired.

I also tried setting the args.IsValid to false but even then my Page.Isvalid always returned to be true.
Any idea what I might be doing wrong?
Tags
Calendar
Asked by
newbie
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
newbie
Top achievements
Rank 1
Share this question
or