www.webdeveloper.com
Results 1 to 2 of 2

Thread: Javascript problem with alert box

  1. #1
    Join Date
    Jul 2012
    Posts
    1

    Javascript problem with alert box

    I have this pice of code:

    Code:
    	function validator() {
    	var l1 = document.calculation.l1.value;
    	var l2 = document.calculation.l2.value;
    	var right = document.calculation.right.value;
    	 
     
    if (l1 == null || l1 == "") alert("error ");
    else if (l1 == null || l2 == "") alert("error");
    else if (l1 == null || right == "") alert("error");
    else if (right == 1) { alert("Correct"); }
     
      else { alert("Incorrect incorect"); } 
     
    return false;
    };
    In Firefox is ok, but in Chrome the alert box is displayed before pressing the Submit button from my form. What can be wrong here?

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,412

    Lightbulb

    What does your <form> tag look like?
    Might need to be
    Code:
    <form name="myForm" action="???" method="post" onsubmit="return validator()">
    Might try changing the following (I'm not sure I understand your logic or needs here):
    Code:
    var flag = true;
      if (l1 == null || l1 == "") { flag=false; alert("error "); }
      if (l2 == null || l2 == "") { flag=false; alert("error"); }
      if (right == null || right == "") { flag=false; alert("error"); }
      if ((flag == true) && (right == 1)) { alert("Correct"); }
      if (flag == false) { alert("Incorrect incorect"); } 
    
      return flag;

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles