We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

javascript cancel

pratikshahse
pratikshahse asked
on
Medium Priority
238 Views
Last Modified: 2010-04-17
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
Comment
Watch Question

Kiran Paul VJComputer Engineer
CERTIFIED EXPERT

Commented:
<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>
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Kiran Paul VJComputer Engineer
CERTIFIED EXPERT

Commented:

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;
}
Kiran Paul VJComputer Engineer
CERTIFIED EXPERT

Commented:

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
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
Kiran Paul VJComputer Engineer
CERTIFIED EXPERT

Commented:
Kiran Paul VJComputer Engineer
CERTIFIED EXPERT

Commented:
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.