I have a two textboxes t1 and t2 . I would like to figure out how in jquery if the values in t1 and t2 are same I can display an alert message. If user does not change the values I want to prevent the form from being submitted.
-
What exactly is your problem?Felix Kling– Felix Kling2012-06-11 12:28:24 +00:00Commented Jun 11, 2012 at 12:28
-
So the user shouldn't be able to submit the form if the values are the same?IMTheNachoMan– IMTheNachoMan2016-11-03 19:44:49 +00:00Commented Nov 3, 2016 at 19:44
Add a comment
|
5 Answers
Try something like this
$('form').submit(function(evt) {
if ($('#textbox1').val() === $('#textbox2').val()) {
alert('values match');
evt.preventDefault();
}
}
uses the .submit() method to bind to the submit event for the form, then compares the 2 values from the textboxes - if they are the same it displays an alert then prevents the default action (form submission) using event.preventDefault()
2 Comments
user1196392
i m using asp.net.and making form in .ascx page.in ascx page i m not using form. i m using <asp:pannel>
Manse
@user1196392 add your HTML output (not asp.net source) to your question - plus any JavaScript you currently have doing what you are trying to do.
$("form").submit(function() {
var _txt1 = $('#txt1').val();
var _txt2 = $('#txt2').val();
if (_txt1 == _txt2)
{
alert('Matching!');
return true;
}
else
{
alert('Not matching!');
return false;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="#" method="post" name="form_name" id="form_id">
<input type="text" id="txt1" name="txt1" />
<input type="text" id="txt2" name="txt2"/>
<input type="submit" value="Submit">
</form>
Comments
jQuery
$(document).ready(function() {
var _t1 = $("#t1");
var _t2 = $("#t2");
$(_t2).focusout(function() {
if(_t1.val() === _t2.val()){
return alert('Match');
}
return alert('No match');
});
});
HTML
<input id="t1">
<input id="t2">
Comments
<script>
function myfunction(){
var val1=document.getElementById('pwd').value;
var val2=document.getElementById('cpwd').value;
if(val1!=val2){
document.getElementById('match').innerHTML="Your password is unmatch";
return false;
}
return true;
}
</script>
2 Comments
E. Zeytinci
Please explain your answer.
Mehran Ayub
'pwd' and 'cpwd' are names of textboxes in html.we get values from these two textboxes and save in two different variables through javascript.Then we match these two variables ,if same then right and if not same then we show a text on html page through this javascript query.document.getElementById('match').innerHTML="Your password is unmatch"; Here match is id of div defined in html code.