enter key with invalid date

3 posts, 0 answers
  1. newbie
    newbie avatar
    355 posts
    Member since:
    Feb 2008

    Posted 03 Feb 2010 Link to this post

    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. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 04 Feb 2010 Link to this post

    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.
  3. newbie
    newbie avatar
    355 posts
    Member since:
    Feb 2008

    Posted 04 Feb 2010 Link to this post

    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?
Back to Top