• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

javascript cancel

I got two variables  and I want to compare the values of those two variables and if variable 2 is not equal to variable 1 then i want a message box saying you cannot continue.

dim variable1 as double
dim variable2 as double.

The value of these two variable changes whenever a user puts values on the form. But when the user presses a button it has to compare the values and if they are not same then a message box should pop up.

if variable2 = variable1

msgbox
else
do rest
end if

i am using asp.net with vb.net code.

please help.

Pratik
0
pratikshahse
Asked:
pratikshahse
  • 5
  • 2
1 Solution
 
Kiran Paul VJComputer EngineerCommented:
<html xmlns="http://www.w3.org/1999/xhtml">
<script language="javascript">
function compare1()
{
      if(document.form1.pass1.value!=document.form1.pass2.value)
      {
            alert('Passwords does not match');
            return false;
      }
return true;
}
</script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form name="form1" method="post" onsubmit="return compare1()">
<p>
  <input name="pass1" type="text" id="pass1" />
</p>
<p>
  <input name="pass2" type="text" id="pass2" />
</p>
<p>
  <input type="submit" name="Submit" value="Submit" />
</p>
</form>
</body>
</html>
0
 
nayernaguibCommented:
You can use this trick:

        If firstValue = secondValue Then
            Dim script As String = "<script>alert(""Values are equal"")</script>"
            Response.Write(script)
        Else
            'do other things
        End If

_______________

  Nayer Naguib
0
 
Kiran Paul VJComputer EngineerCommented:

oops little problem , put the script inside the head tag.
u can also check if the fields are empty by

function compare1()
{
 if(document.form1.pass1.value=='')
 {
  alert("Please enter a value in the fieldname");
  return false;
  }
if(document.form1.pass2.value=='')
 {
   alert("Please enter a value in the fieldname");
   return false;
  }    
if(document.form1.pass1.value!=document.form1.pass2.value)
     {
          alert('Passwords does not match');
          return false;
     }
return true;
}
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Kiran Paul VJComputer EngineerCommented:

the main part is that in the form onsubmit event u should call the function like this
onsubmit="return compare1()"
otherwise it wont work correctly
0
 
nayernaguibCommented:
MsgBox does not work in ASP .NET simply because ASP .NET code executes on the server, not inside your browser. Your browser only renders the HTML result of the code execution. By using the piece of code that I posted above, you write a single line of JavaScript in the HTML document to be displayed by the browser, which causes the browser to execute the script, displaying the required message.

_______________

  Nayer Naguib
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now