I am trying to write jQuery code to check if two inputs have the same value in the submit form with no luck.
If input with id1 has the same meaning as input with id2, "some text" returns false.
Any help would be greatly appreciated.
$('#form').submit(function() { var id1 = $(#id1).text(); var id2 = $(#id2).text(); if (id1 == id2) { alert('Error, cant do that'); return false; } else { return true; } });
DEMO HERE
<input type="text" id="id1" /> <input type="text" id="id2" /> $('input').blur(function() { if ($('#id1').attr('value') == $('#id2').attr('value')) { alert('Same Value'); return false; } else { return true; } });
I just used blur, not shape.
It's quite simple, just compare with == and input values. Put this inside the submit() your form.
==
submit()
var match = $('#id1').val() == $('#id2').val();
If match is false , you can show your alert() and event.preventDefault() .
match
false
alert()
event.preventDefault()
Perhaps you have missed code, try replacing $(#id1) with $('#id1') so that from $(#id2) to $('#id2')
$(#id1)
$('#id1')
$(#id2)
$('#id2')
Fixed code
$('#form').submit(function() { var id1 = $('#id1').text(); //if #id1 is input element change from .text() to .val() var id2 = $('#id2').text(); //if #id2 is input element change from .text() to .val() if (id1 == id2) { alert('Error, cant do that'); return false; } else { return true; } });
Source: https://habr.com/ru/post/1388790/More articles:Remove request argument from url in javascript - javascriptissubclass throws an exception if the first argument is not a class - pythonApply the same effect to all elements with the same tabindex in the focus of one of them - cssIs there something like python issubclass that will return False if the first arg is not a class? - pythonShould I use the POST request on / resources / or PUT request on / resources / id / to create a new object? - restIs it possible to install Launch4J so that the exe files that it creates cannot display its contents through a zip / egg / rar file when you click on it / open it? - javaHow to create a system for migrating encryption? - passwordsRails 3.1 Possible error in the pipeline and Uglifier - ruby-on-rails-3.1Java FIFO Queue with disk overflow - javaSystem.IndexOutOfRangeException: The index was outside the array - c #All Articles