admin管理员组文章数量:1296878
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.
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 Commented Jun 11, 2012 at 12:28
- So the user shouldn't be able to submit the form if the values are the same? – IMTheNachoMan Commented Nov 3, 2016 at 19:44
5 Answers
Reset to default 1Try 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 pares 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()
Working example here
$("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./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>
example link
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">
Fiddle https://jsfiddle/ptda108k/1
Here is the code
if(!$("#t1").val() && !$("#t2").val() && $("#t1").val() === $("#t2").val()){
alert("Both values are same");
}else{
return false;
}
I am doing strict type check for input values by ===.
Hope this will help your requirement.
<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>
本文标签: javascripthow to compare two text input type box values using jqueryStack Overflow
版权声明:本文标题:javascript - how to compare two text input type box values using jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741644076a2390084.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论