jquery - Check form for similar value before submit -
is there anyway check if similar value exist in input box
ex:
<form> user 1 <input name="user1" value="bla1"> <input name="pass1" value="ple1"> <input name="email1" value="foo1@yy.cc"> user 2 <input name="user2" value="bla2"> <input name="pass2" value="ple1"> <input name="email2" value="foo2@yy.cc"> user 3 <input name="user3" value="bla1"> <input name="pass3" value="pla3"> <input name="email3" value="foo2@yy.cc"> <button type="submit"> </form>
the verification trigger when form submitted, alert user , add class both input similar value. input fields starts @ blank , user should input values. fields should compared type user,pass, , email
i've put wee jsfiddle you.
basically loops through every element in form, finds similar names, , if have same value marks them both. little inefficient compares b, b a, still works well.
$('form').submit(function(event) { var ok = true, $form = $(this), $inputs = $(this).find('input') $inputs.removeclass('error') $inputs.each(function() { var $src = $(this) //get name of element, , strip off number var name = $(this).attr('name').replace(/[0-9]/g,'') //filter inputs down ones name starts same $inputs.not(this).filter('[name^="' + name + '"]').each(function() { if($src.val() == $(this).val()) { ok = false $(this).addclass('error') $src.addclass('error') } }) }) if(!ok) event.preventdefault() })