Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Suppress onblur validation alert on window close

    Hi everyone. New programmer here trying to get fix this one specific issue I am having and I could use some help. Using Javascript, ASP.net with C# code behind.

    I am validating a textbox using the onblur event. It is being validated in that it must have an alphanumeric entry before the user tabs off of it to the next box.

    The validation part works, the part that doesn't work is that I get the validation alert textbox if I close the window. I have a workaround in that I am assigning that textbox a character using windo.onberforeunload but I know that isn't right.

    Here is what I have so far...

    Code behind on Page_Load...

    Code:
    txtCLBRTNWC.Attributes.Add("onblur", "return reqVLD(this)");
    here is my textbox field...

    Code:
                       <asp:TextBox ID="txtCLBRTNWC" runat="server" Width="75" MaxLength="4"
                           ontextchanged="txtCLBRTNWC_TextChanged" TabIndex="1"
                           onkeyup="clbwclngCHK(this);" ></asp:TextBox>
    And here are the 2 functions being used...
    Code:
    function reqVLD(alphanumericChar)
    {
        var chk = /^[a-zA-Z0-9]+$/;
    //    var boxid = document.getElementById("<%=txtCLBRTNWC.ClientID%>").value;
    //    var matchArray = boxid.match(chk);
        if(document.getElementById("<%=txtCLBRTNWC.ClientID%>").value.match(chk))
        {
        return true;
        }
        else
        {
        document.getElementById("<%=txtCLBRTNWC.ClientID%>").focus();
        document.getElementById("<%=txtCLBRTNWC.ClientID%>").value = "";
        alert("Please enter a Work Center.");
        return false;
        }   
    }
    
    function clbwclngCHK(obj_in)//When txtCLBRTNWC entry hits the max of 4 auto focuses to next box.
    {
    if (obj_in.value.length == 4) document.getElementById("<%=txtSTRTDT.ClientID%>").focus();
    }
    Here is my work around...
    Code:
    window.onbeforeunload = function ()
    {                                  
    document.getElementById("<%=txtCLBRTNWC.ClientID%>").value = "1";
    }
    Summary. User has to enter something in box before they can move on. This is checked and validating using an onblur event. Problem is, the onblur event fires if closing\exiting the window.

    I know there has to be a better way of doing this. Any advise or help would be greatly appreciated.

    Thanks
    Will
    Last edited by wcdturbo; 12-11-2009 at 03:07 PM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,901
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    Well, it's still a hack, but maybe a more reasonable hack. How about this?

    Code:
    var doingUnload = false;
    window.onbeforeunload = function ()
    {                                  
        doingUnload = true;
    }
    
    function reqVLD(alphanumericChar)
    {
        if ( doingUnload ) return true;
        ... rest of function as is ...
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    wcdturbo (12-11-2009)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    To expand the above:-


    Code:
    <script type = "text/javascript">
    
    var doingUnload = false;
    window.onbeforeunload = function (){                                  
    doingUnload = true;
    }
    
    function testit() { 
    if (doingUnload) {return true};                                                          
    var val = document.getElementById("uname").value;
    val = val.replace(/^\s+|\s+$/g,"");   // strip leading and trailing spaces
    val = val.replace(/\s{2,}/g," "); /// replace multiple spaces by single space
    val = val.replace(/[^a-zA-Z0-9\s]/g,"");  // strip non-alphanumerics
    if (val.length < 3) {
    alert ("You must enter your name");
    document.getElementById("uname").value = "";
    myfield = document.getElementById("uname");  // overcome bug in Firefox - note myfield must be a global variable
    setTimeout('myfield.focus();myfield.select();' , 10);
    return false;
    }
    }
    
    </script>

    Do please read the posting guidelines regarding silly thread titles. The thread title is supposed to help people who have a similar problem in future. Yours is useless for this purpose. You can (and should) edit it to make it more meaningful. I suggest "Suppress onblur validation alert on window close".
    Last edited by Philip M; 12-11-2009 at 08:35 AM. Reason: Add regexes

  • Users who have thanked Philip M for this post:

    wcdturbo (12-11-2009)

  • #4
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    To expand the above:-


    Code:
    <script type = "text/javascript">
    
    var doingUnload = false;
    window.onbeforeunload = function (){                                  
    doingUnload = true;
    }
    
    function testit() { 
    if (doingUnload) {return true};                                                          
    var val = document.getElementById("uname").value;
    val = val.replace(/^\s+|\s+$/g,"");   // strip leading and trailing spaces
    val = val.replace(/\s{2,}/g," "); /// replace multiple spaces by single space
    val = val.replace(/[^a-zA-Z0-9\s]/g,"");  // strip non-alphanumerics
    if (val.length < 3) {
    alert ("You must enter your name");
    document.getElementById("uname").value = "";
    myfield = document.getElementById("uname");  // overcome bug in Firefox - note myfield must be a global variable
    setTimeout('myfield.focus();myfield.select();' , 10);
    return false;
    }
    }
    
    </script>

    Do please read the posting guidelines regarding silly thread titles. The thread title is supposed to help people who have a similar problem in future. Yours is useless for this purpose. You can (and should) edit it to make it more meaningful. I suggest "Suppress onblur validation alert on window close".

    Sorry Phil Didnt read that, tried to edit heading per suggestion but I can't?

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by wcdturbo View Post
    Sorry Phil Didnt read that, tried to edit heading per suggestion but I can't?
    Just click the blue edit button, then change the title which is listed above the message box.

    You did it - good man!
    Last edited by Philip M; 12-11-2009 at 03:10 PM.

  • #6
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Just click the blue edit button, then change the title which is listed above the message box.

    Actually, had to hit the edit button twice. First time allows me to edit just the message. Second time allows me to edit everything.

    Thanks for you help so far. I ran into one more problem with this. I have a clear function that clears out the various textboxes if a user wants to start over. That clear function causes the onblur to happen.

    here is what it looks like...
    Code:
    protected void clearFunc()
        {
            txtCLBRTNWC.Text = "";
            txtCOMPLDT.Text = "";
            txtENDDT.Text = "";
            txtESTMNHRS.Text = "";
            txtEXPNDMNHRS.Text = "";
            txtITMCOMPLT.Text = "";
            txtITNJB.Text = "";
            txtGGG.Enabled = true;
            txtGGG.Text = "";
            txtLSFILMNT.Text = "";
            txtRFTPRD.Text = "";
            txtSTRTDT.Text = "";
            btnADD.Enabled = false;
            btnCHNG.Enabled = false;
            btnDLT.Enabled = false;
            btnEXT.Enabled = false;
            btnCLR.Enabled = false;
            lblCAL.Visible = false;
            MGGUPDT.Visible = false;
            btnCLNDRSTRTDT.Visible = false;
            btnCLNDRCMPDT.Visible = false;
            btnCLNDRENDDT.Visible = false;
            MGGBCMAIN.Visible = true;
            MGGERR.Visible = false;
            MGGNOCHNG.Visible = false;
            MGGNOUIC.Visible = false;
            MGGINVLDJCN.Visible = false;
            Page.SetFocus(txtGGG);
    Here is where the onblur is being called...

    Code:
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Page.SetFocus(txtGGG);
                lblCAL.Visible = false;
                txtESTMNHRS.ReadOnly = true;
                txtEXPNDMNHRS.ReadOnly = true;
                txtITNJB.ReadOnly = true;
                txtITMCOMPLT.ReadOnly = true;
                txtLSFILMNT.ReadOnly = true;
                btnCLNDRSTRTDT.Visible = false;
                btnCLNDRCMPDT.Visible = false;
                btnCLNDRENDDT.Visible = false;
                M24UPDT.Visible = false;
                M24ERR.Visible = false;
                M24NOCHNG.Visible = false;
                M24NOUIC.Visible = false;
                M24INVLDJCN.Visible = false;
                txtCLBRTNWC.Attributes.Add("onblur", "return reqVLD(this)");
                txtSTRTDT.Attributes.Add("onclick", "calendar_window=window.open('calendar.aspx?formname=aspnetForm.ctl00$ContentPlaceHolder1$txtSTRTDT','calendar_window','width=178,height=188,left=550,top=240');calendar_window.focus()");
                txtENDDT.Attributes.Add("onclick", "calendar_window=window.open('calendar.aspx?formname=aspnetForm.ctl00$ContentPlaceHolder1$txtENDDT','calendar_window','width=178,height=188,left=550,top=240');calendar_window.focus()");
                txtCOMPLDT.Attributes.Add("onclick", "calendar_window=window.open('calendar.aspx?formname=aspnetForm.ctl00$ContentPlaceHolder1$txtCOMPLDT','calendar_window','width=178,height=188,left=550,top=240');calendar_window.focus()");
            }
        }
    The solution you both provided for the Window close works great. Just ran into it again though with my clear button.
    Last edited by wcdturbo; 12-11-2009 at 03:14 PM.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Why should that happen? If the user clicks on a button the textbox has lost focus and function testit() has been called.


    Code:
    Name <input type = "text" name = "uname" id = "uname" onblur = "testit()" />
    <br>
    <input type = "button" value = "Clear the form" onclick = "clearForm()">
    
    <script type = "text/javascript">
    
    function clearForm() {
    document.getElementById("uname").value = "";
    }
    var doingUnload = false;
    window.onbeforeunload = function (){                                  
    doingUnload = true;
    }
    
    function testit() { 
    if (doingUnload) {return true};                                                          
    var val = document.getElementById("uname").value;
    val = val.replace(/^\s+|\s+$/g,"");   // strip leading and traling spaces
    val = val.replace(/\s{2,}/g," "); /// replace multiple spaces by single space
    val = val.replace(/[^a-zA-Z0-9\s]/g,"");  // strip non-alphanumerics
    if (val.length < 3) {
    alert ("You must enter your name");
    document.getElementById("uname").value = "";
    myfield = document.getElementById("uname");  // note myfield must be a global variable
    setTimeout('myfield.focus();myfield.select();' , 10);  // overcome bug in Firefox
    return false;
    }
    }
    
    </script>

  • #8
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Okay, I think I have an idea on what is going on.

    here is the modified code based on what you posted.

    Code:
    function reqVLD(alphanumericChar) 
        { 
        if (doingUnload) {return true};
        var chk = /^[a-zA-Z0-9]+$/;                                                          
        var val = document.getElementById("<%=txtCLBRTNWC.ClientID%>").value;
        val = val.replace(/^\s+|\s+$/g,"");   // strip leading and traling spaces
        val = val.replace(/\s{2,}/g," "); /// replace multiple spaces by single space
        val = val.replace(/[^a-zA-Z0-9\s]/g,"");  // strip non-alphanumerics
            if(document.getElementById("<%=txtCLBRTNWC.ClientID%>").value.match(chk)) 
                {
                return true;
                }
                else
                {
                //alert("Please enter a Work Center.");
                document.getElementById("<%=txtCLBRTNWC.ClientID%>").value = "";
                myfield = document.getElementById("<%=txtCLBRTNWC.ClientID%>");  // note myfield must be a global variable
                setTimeout('myfield.focus();myfield.select();' , 10);  // overcome bug in Firefox
            return false;
                }
        }
    Notice I have alert("Please enter a Work Center."); commented out. That is because it works with it commented out however, if they tab off of the testbox without entering any data they no longer get the alert because I have it commented out. If I don't comment it out whenever they click on the clear button the alert pops but doesn't clear the fields.

    So, if I comment it out it works except if they tab off the focused box it just returns focus to that box and doesn't tell them why.

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    What has happened to the code and function clearForm() which I posted in Post #7?

    Code:
    Name <input type = "text" name = "uname" id = "uname" onblur = "testit()" />
    <br>
    <input type = "button" value = "Clear the form" onclick = "clearForm()">
    
    <script type = "text/javascript">
    
    function clearForm() {
    document.getElementById("uname").value = "";
    }
    It makes it very difficult if you only post part of your code. If you post the whole thing including the (relevant) HTML it is much easier for me to test it.

  • #10
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I modified the TestIt function slightly to the code in the previous post. It is now reqVLD.

    The problem is, this program is a few hundred+ lines between asp and C#. I am not sure what I need to post and what I don't. So, I'll try and give you what I think you need.


    Here are the JS functions.(I modified what you posted in 7)
    Code:
    function clearForm() 
    {
        document.getElementById("<%=txtCLBRTNWC.ClientID%>").value = "";
            }
                var doingUnload = false;
                window.onbeforeunload = function ()
            {                                  
            doingUnload = true;
        }
    
    function reqVLD(alphanumericChar) 
        { 
        if (doingUnload) {return true};
        var chk = /^[a-zA-Z0-9]+$/;                                                          
        var val = document.getElementById("<%=txtCLBRTNWC.ClientID%>").value;
        val = val.replace(/^\s+|\s+$/g,"");   // strip leading and traling spaces
        val = val.replace(/\s{2,}/g," "); /// replace multiple spaces by single space
        val = val.replace(/[^a-zA-Z0-9\s]/g,"");  // strip non-alphanumerics
            if(document.getElementById("<%=txtCLBRTNWC.ClientID%>").value.match(chk)) 
                {
                return true;
                }
                else
                {            
                document.getElementById("<%=txtCLBRTNWC.ClientID%>").value = "";
                myfield = document.getElementById("<%=txtCLBRTNWC.ClientID%>");  // note myfield must be a global variable
                setTimeout('myfield.focus();myfield.select();' , 10);  // overcome bug in Firefox
                alert("Please enter a Work Center.");     
            return false;     
                }         
        }

    Here are the TXT/BTN.
    Code:
                        <asp:TextBox ID="txtCLBRTNWC" runat="server" Width="75" MaxLength="4" 
                           ontextchanged="txtCLBRTNWC_TextChanged" TabIndex="1" ></asp:TextBox>
    Code:
    <asp:Button ID="btnCLR" runat="server" AccessKey="l" Text="Clear" OnClick="btnCLR_Click" />
    Here is the code behind...
    Code:
    protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Page.SetFocus(txtJCN);
                lblCAL.Visible = false;
                txtESTMNHRS.ReadOnly = true;
                txtEXPNDMNHRS.ReadOnly = true;
                txtITNJB.ReadOnly = true;
                txtITMCOMPLT.ReadOnly = true;
                txtLSFILMNT.ReadOnly = true;
                btnCLNDRSTRTDT.Visible = false;
                btnCLNDRCMPDT.Visible = false;
                btnCLNDRENDDT.Visible = false;
                M24UPDT.Visible = false;
                M24ERR.Visible = false;
                M24NOCHNG.Visible = false;
                M24NOUIC.Visible = false;
                M24INVLDJCN.Visible = false;
                btnCLR.Attributes.Add("onclick", "clearForm()");
                txtCLBRTNWC.Attributes.Add("onblur", "return reqVLD(this)");
            }
        }
    Code:
        protected void btnCLR_Click(object sender, EventArgs e)
        {
            clearFunc();
        }
    Code:
    protected void clearFunc()
        {
            txtCLBRTNWC.Text = "";
            txtCOMPLDT.Text = "";
            txtENDDT.Text = "";
            txtESTMNHRS.Text = "";
            txtEXPNDMNHRS.Text = "";
            txtITMCOMPLT.Text = "";
            txtITNJB.Text = "";
            txtJCN.Enabled = true;
            txtJCN.Text = "";
            txtLSFILMNT.Text = "";
            txtRFTPRD.Text = "";
            txtSTRTDT.Text = "";
            btnADD.Enabled = false;
            btnCHNG.Enabled = false;
            btnDLT.Enabled = false;
            btnEXT.Enabled = false;
            btnCLR.Enabled = false;
            lblCAL.Visible = false;
            M24UPDT.Visible = false;
            btnCLNDRSTRTDT.Visible = false;
            btnCLNDRCMPDT.Visible = false;
            btnCLNDRENDDT.Visible = false;
            M24BCMAIN.Visible = true;
            M24ERR.Visible = false;
            M24NOCHNG.Visible = false;
            M24NOUIC.Visible = false;
            M24INVLDJCN.Visible = false;
            Page.SetFocus(txtJCN);
        }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •