Click here to Skip to main content
15,908,264 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi all

I need to validate 2 text-box's value like compare validater. Please tell me javascript code to do that.
Posted
Comments
Sergey Alexandrovich Kryukov 13-Aug-12 1:38am    
Again, not a question. What does it mean "tell me JavaScript code"? To write it for your?
--SA
[no name] 13-Aug-12 1:39am    
As we do in ASP.NET compare validater to check 2 text-box value so the same thing how to do in ASP
Joan M 13-Aug-12 5:21am    
As in PHP I guess that ASP will allow you to check/validate the values without calling to javascript. In my web page I validate using javascript (in case it is enabled at the client browser) and in PHP (I only reach that if the javascript has accepted the values or it was disabled. Consider that to handle those checks.

This is very easy:-
1. Write a JavaScript function.
2. Retrieve the value of your both textbox one by one
3. And then compare it.
4. After comparing if it returns false then display a alert message.
5. At last call this function on onblur event of your second textbox.

Following link might help you:
JavaScript String Compare[^]

Now go ahead.
All the best.
 
Share this answer
 
v3
C#
function ValidateForm() {

    var errorMessage = "<br><b>please enter</b>";
    var password = document.getElementById("txtPassword").value;
    var confirmPassword = document.getElementById("txtConfirmPassword").value;
  
    if (password.length == 0) {
        errorMessage += "<br><b>Password</b>";
    }
    if (password != confirmPassword) {
        errorMessage = errorMessage + "<br><br><b>Matching Passwords</b><br>";
    }
    if (errorMessage != "<br><b>please enter</b>") {
        validation.innerHTML = errorMessage;
        return false;
    }
    else {
        validation.innerHTML = "";
        return true;
    }
}


here "validation" is the id of a div element into which error msg is diaplayed as inner html...
 
Share this answer
 
v3
Try this:
Javascript:
JavaScript
function fnCompare()
{
    var txt1 = document.getElementById("txt1");
    var txt2 = document.getElementById("txt2");
    if(txt1.value == txt2.value){
        alert("Success!!");
    }
    else{
        alert("Error!!");
    }
}

HTML:
HTML
<input type="text" id="txt1" onblur="fnCompare();"/>
<input type="text" id="txt2" onblur="fnCompare();"/>



--Amit
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900