jquery - HTML5 set input error manually from javascript -
i'm trying make form user has select box choose value from.
but has input field can input text himself, , value requiered, putting 'required' on form submit give error, in case need check if input has value not give error.
i'm making $each() function on every selects , it's working, have console.log sayng witch of them required , select or input empty.
what need use normal input value missing standard html5, 1 shows when put 'required' tag on inputs, sayng default message.
this code have rigth if helps, it's triggered when users click submyt button way
$("input[name='guardar']").click(function() { $("select").each(function(key, valeu) { var requerido = $(this).attr('requerido'); var nome = $(this).attr('name'); var input = $("input[name='"+nome+"_default']").val(); if(requerido == "true"){ //tem de ver se ou select tem valores, ou o input seguinte if($(this).val().length == 0 && input.length == 0){ console.log(nome+" empty"); //make select box show empty value message??? } else { console.log(nome+" tem valor "+input); } } }); });
thanks
actualy used way of doing it.
$("input[name='guardar']").click(function() { $("select").each(function(key, valeu) { var requerido = $(this).attr('required'); //console.log(requerido); var nome = $(this).attr('name'); var input = $("input[name='" + nome + "_default']").val(); if (typeof requerido !== typeof undefined) { //tem de ver se ou select tem valores, ou o input seguinte if ($(this).val().length > 0 || input != "") { $(this).removeattr('required'); } } }); });
basically search every 'required' filed then, watch input next him , if has value on it, removes 'required' tag , works yust fine :)
Comments
Post a Comment