...

View Full Version : Suppress onblur validation alert on window close



wcdturbo
12-10-2009, 09:26 PM
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...


txtCLBRTNWC.Attributes.Add("onblur", "return reqVLD(this)");

here is my textbox field...


<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...


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...


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

Old Pedant
12-10-2009, 10:29 PM
Well, it's still a hack, but maybe a more reasonable hack. How about this?



var doingUnload = false;
window.onbeforeunload = function ()
{
doingUnload = true;
}

function reqVLD(alphanumericChar)
{
if ( doingUnload ) return true;
... rest of function as is ...
}

Philip M
12-11-2009, 07:27 AM
To expand the above:-



<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".

wcdturbo
12-11-2009, 01:38 PM
To expand the above:-



<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?

Philip M
12-11-2009, 03:04 PM
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!

wcdturbo
12-11-2009, 03:10 PM
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...

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...


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.

Philip M
12-11-2009, 03:20 PM
Why should that happen? If the user clicks on a button the textbox has lost focus and function testit() has been called.



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>

wcdturbo
12-11-2009, 03:42 PM
Okay, I think I have an idea on what is going on.

here is the modified code based on what you posted.


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.

Philip M
12-11-2009, 04:10 PM
What has happened to the code and function clearForm() which I posted in Post #7?


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.

wcdturbo
12-11-2009, 04:24 PM
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)

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.

<asp:TextBox ID="txtCLBRTNWC" runat="server" Width="75" MaxLength="4"
ontextchanged="txtCLBRTNWC_TextChanged" TabIndex="1" ></asp:TextBox>

<asp:Button ID="btnCLR" runat="server" AccessKey="l" Text="Clear" OnClick="btnCLR_Click" />

Here is the code behind...

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)");
}
}

protected void btnCLR_Click(object sender, EventArgs e)
{
clearFunc();
}

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);
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum