By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,034 Members | 1,028 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,034 IT Pros & Developers. It's quick & easy.

doubt in executing a function: alert box appears twice instead of once

P: 9
hi ,
I have written a javascript function like this.

Expand|Select|Wrap|Line Numbers
  1.      function call(frm)
  2. {
  3.     var loc_name = document.frm.loca.value;
  4.     //alert('<'+loc_name+'>');
  5.     //var objRegExp = /^(\s*)(\b[\w\W]*)/;
  6.     //if(objRegExp.test(loc_name)) {
  7.     //alert("got it");
  8.     //loc_name = loc_name.replace(objRegExp, '$2');  }
  9.  
  10.     if(loc_name=='' || loc_name==' ' || loc_name=='   ' || loc_name=='    ' || loc_name=='    ' || loc_name=='       ')
  11.     {
  12.           alert('Enter location');
  13.     }
  14.     else
  15.     {
  16.         var url="http://10.202.1.24/shiftscheduler/loca_save";
  17.         document.frm.action=url;
  18.         document.frm.submit();
  19.     }
  20. }
  21.  
Actually i am getting location value from a text box. I have used onmouse over function. When i have placed mouse on the button,
the function call is called. when i do not enter anything in the text field and just place the mouse over the button, i am getting alert boxes two times with message as "Enter location". Actually if i enter any text in the textbox then i need to send those value to other page. If i just place the mouse over the button without entering anything in the textfield, i have to get an alert message saying " Enter location" , only once. Could anybody tell me the reason y is it behaving like that.

Thanks,
pavan
Apr 29 '08 #1
Share this Question
Share on Google+
3 Replies


acoder
Expert Mod 15k+
P: 16,027
Firstly, show the HTML code where you call the function.

Secondly, you should be using onclick rather than onmouseover.

Thirdly, I know what you're trying to do with the space checks, but it's ghastly! Change it to use a regular expression as you've made an attempt to do with the commented code.

Finally, please use [code] tags when posting code.
Apr 29 '08 #2

P: 9
hi,
Thanks for ur reply.I have tried the regular expression which i have commented, and also tried to use it, but it has not worked. I am giving u the html code which is as follows:
[html]
<html>
<head>
<title>Location Master</title>
<link rel="stylesheet" type="text/css" href="http://10.202.1.24/css/Scheduler.css"/>
<script type="text/javascript">
function call(frm)
{
var loc_name = document.frm.loca.value;
//alert('<'+loc_name+'>');
//var objRegExp = /^(\s*)(\b[\w\W]*)/;
//if(objRegExp.test(loc_name)) {
//alert("got it");
//loc_name = loc_name.replace(objRegExp, '$2'); }

if(loc_name=='' || loc_name==' ' || loc_name==' ' || loc_name==' ' || loc_name==' ' || loc_name==' ')
{
alert('Enter location');
var url="http://10.202.1.24/shiftscheduler/location_screen";
document.frm.action=url;
document.frm.submit();

}
else
{
var url="http://10.202.1.24/shiftscheduler/loca_save";
document.frm.action=url;
document.frm.submit();
}
}
function cal(frm)
{
document.frm.loca.value='';
}
</script>
</head>
<body>
<form name='frm' method='post'>
<table align="center">
<tr><td align="center">Location master screen</td>
</table>
<br>
<table align="center">
<tr> <td>
start
if($p==1)
{
print "Location Inserted";
}
print <<start;
</td> </tr>
</table>
<table align="center" width=40% height=20% border=1>
<br><br>
<tr><td align="center"><input type="text" name="loca"></td></tr>
<tr><td align="center"><input type="submit" name="butt" value="Insert" onmouseover='call(this.form)' onmouseout='cal(this.form)'></td></tr>
</table>
</form>
</body>
</html>
[/html]

here $p=1 is a perl script command. I am using onmouseover and onmouseout, both. I am getting the alert box twice as and when i don't enter any text and put the mouse over the button.
Could u pls tell me the problem, y the code is behaving like that.

Thanks once again,
pavan.
Apr 29 '08 #3

acoder
Expert Mod 15k+
P: 16,027
I am using onmouseover and onmouseout, both. I am getting the alert box twice as and when i don't enter any text and put the mouse over the button.
Could u pls tell me the problem, y the code is behaving like that.
Why use onmouseover/out when a button is there to be clicked? Use onclick instead.

You're also submitting the form when there's an error and the location hasn't been entered.
Apr 29 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.