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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pavanpvss
    New Member
    • Apr 2008
    • 9

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

    hi ,
    I have written a javascript function like this.

    [CODE=javascript] function call(frm)
    {
    var loc_name = document.frm.lo ca.value;
    //alert('<'+loc_n ame+'>');
    //var objRegExp = /^(\s*)(\b[\w\W]*)/;
    //if(objRegExp.te st(loc_name)) {
    //alert("got it");
    //loc_name = loc_name.replac e(objRegExp, '$2'); }

    if(loc_name=='' || loc_name==' ' || loc_name==' ' || loc_name==' ' || loc_name==' ' || loc_name==' ')
    {
    alert('Enter location');
    }
    else
    {
    var url="http://10.202.1.24/shiftscheduler/loca_save";
    document.frm.ac tion=url;
    document.frm.su bmit();
    }
    }
    [/CODE]

    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
    Last edited by acoder; Apr 29 '08, 01:34 PM. Reason: Added code tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

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

    Comment

    • pavanpvss
      New Member
      • Apr 2008
      • 9

      #3
      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>Locati on 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.lo ca.value;
      //alert('<'+loc_n ame+'>');
      //var objRegExp = /^(\s*)(\b[\w\W]*)/;
      //if(objRegExp.te st(loc_name)) {
      //alert("got it");
      //loc_name = loc_name.replac e(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.ac tion=url;
      document.frm.su bmit();

      }
      else
      {
      var url="http://10.202.1.24/shiftscheduler/loca_save";
      document.frm.ac tion=url;
      document.frm.su bmit();
      }
      }
      function cal(frm)
      {
      document.frm.lo ca.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='ca ll(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.
      Last edited by gits; Apr 29 '08, 03:28 PM. Reason: fix code tags

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Originally posted by pavanpvss
        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.

        Comment

        Working...