...

View Full Version : onBlur problem ?



bewolf
08-11-2006, 07:45 AM
I have two textboxes, each of them is controlled with a function called checknumber. It controlles if they are empty or a valid number and I connected this function to onBlur event of these textboxes. The problem is that when I wrote an invalid number to the first and click the second one, It gives continuously error messages. How can I solve this problem ?

_Aerospace_Eng_
08-11-2006, 07:54 AM
And we are supposed to know what your code looks like how?

RexxCrow
08-11-2006, 10:10 AM
I know the answer to that one, that's easy! ...just stop writing invalid numbers in the boxes. ;) hehee

Do you have a check to ensure the entered number is actually a number and is within a set range and/or is a valid number contained within your variable specs?

bewolf
08-11-2006, 10:36 AM
Yes, I checked my code and controls carefully. It is like that basically;

function controlCode(){

if( !checknumber(document....("text1).value) )
alert("Wrong number!")

}

And checknumber function is the function given at the link:

http://www.javascriptkit.com/script/script2/numc.shtml

I only added one more textbox and instead of controlling from submit button, I added code this "controlCode()" function to the onBlur() event.I didn't add any more code except these two functions.
User will write a valid number and make tab and write another valid number to the second textbox and make another tab and enter submit button.
When I wrote an invalid number to the first box and tab to the second box, an alert window appears and how many time you close it, it continues appearing. How can I solve this problem ?

RexxCrow
08-11-2006, 10:55 AM
That sounds like you have the alert inside a check/validate loop?


This recent topic is similar and should help you with your format:

http://codingforums.com/showthread.php?t=93250

bewolf
08-11-2006, 03:41 PM
Select textbox1 and write an invalid date and then select textbox2, you will see what I am trying to explain.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>

<script type="text/javascript">

function checkdate(input){
var validformat=/^\d{2}\/\d{2}\/\d{4}$/ //Basic check for format validity
var returnval=false
if (!validformat.test(input.value))
alert("Wrong date format!")
else{ //Detailed check for valid date ranges
var dayfield=input.value.split("/")[0]
var monthfield=input.value.split("/")[1]
var yearfield=input.value.split("/")[2]
var dayobj = new Date(yearfield, monthfield-1, dayfield)
if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
alert("Invalid date!")
else
returnval=true
}
if (returnval==false)
input.focus()
return returnval
}

</script>

</head>

<body>
<form id="form1" name="form1" method="post" action="">
<p>&nbsp;</p>
<p>
<label>Text1
<input type="text" name="textfield" onblur="return checkdate(this)" />
</label>
</p>
<p>
<label>Text2
<input type="text" name="textfield2" onblur="return checkdate(this)" />
</label>
</p>
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
<p>&nbsp;</p>
</form>
</body>
</html>

Kor
08-11-2006, 04:23 PM
I would have used a validation based on onsubmit event, not onblur. The way you wrote the code, you enter in a loop, as you need to press OK on that alert, but doing that, you do start again an onblur event, which will produce an alert, and so on
Try this variant:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<script type="text/javascript">
/* <![CDATA[ */
function checkdate(f){
var names=['textfield','textfield2'];
var validformat=/^\d{2}\/\d{2}\/\d{4}$/ //Basic check for format validity
for(var i=0;i<names.length;i++){
if (!validformat.test(f[names[i]].value)){
alert('Wrong date format!');
f[names[i]].focus();
return false;
}
//Detailed check for valid date ranges
var dayfield=f[names[i]].value.split("/")[0]
var monthfield=f[names[i]].value.split("/")[1]
var yearfield=f[names[i]].value.split("/")[2]
var dayobj = new Date(yearfield, monthfield-1, dayfield)
if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield)) {
alert('Invalid date!');
f[names[i]].focus();
return false;
}
}
}
/* ]]> */
</script>

</head>

<body>
<form id="form1" method="post" action="" onsubmit="return checkdate(this)">
<p>&nbsp;</p>
<p>
<label>Text1
<input type="text" name="textfield" />
</label>
</p>
<p>
<label>Text2
<input type="text" name="textfield2" />
</label>
</p>
<p>
<input type="submit" name="sub" value="Submit" />
</p>
<p>&nbsp;</p>
</form>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum