I am developing a way to get a json from all Input that are in a div determined with jQuery em>. This is almost finalized I need only one detail and I want to not show me the json until all fields are validated and also if I do not complete a field that returns me a message I'm missing fill a certain field. I show the code ...
$.fn.DataForms = function(){
var $_model = []
var input = $(this).find('input, textarea, select');
var isComplate = true;
$(input).each(function(ev){
if($(this).attr('type') === "radio"){
if($(this).validateCheck()){
$_model += $(this).attr('name') + ": '" + $(this).val() + "',"
}else{
$('.alert').alertBang('Falta seleccionar una opcion ${$(this).attr('name')}');
isComplate = false;
}
}else if($(this).context.localName === "select"){
if($(this).validateSelect()){
$_model += $(this).attr('name') + ": '" + $(this).val() + "',"
}else{
$('.alert').alertBang('Falta seleccionar la opcion deseada ${$(this).attr('name')}');
isComplate = false;
}
}else{
if($(this).validate()){
$_model += $(this).attr('name') + ": '" + $(this).val() + "',"
}else{
$('.alert').alertBang('Falta rellenar el campo ${$(this).attr('name')}');
isComplate = false;
}
}
})
$_model = JSON.stringify("[{" + $_model + "}]");
$_model = $_model.replace( /"{/gi, '{');
$_model = $_model.replace( /}"/gi, '}')
$_model = $_model.replace(',}', '}');
$_model = $_model.replace( /,null/gi, '');
$_model = eval($_model)
$_model = eval($_model)
if(isComplate === true){
return $_model;
}else{
return
}
}
The Validate () functions are functions created by me that are responsible for seeing if the field was filled in or selected, otherwise it will return false , and the function < em> AlertBang () shows me the message I'm passing you.
My problem concretely is that if we have two 3 input of type radio it is valid for one and for the other 2 it does not. Therefore I would return an alert that a field is not selected and json would never come to light.