views:

253

answers:

5

I'm trying to validate my inputs with jQuery before a form is sent.

This is what my input fields look like when there is no value inserted in the database:

    <input
      class="field"
      type="text"
      style="background-color: rgb(255, 255, 255);"
      value="" name="input_18748_388_1257894000_"/>

This is what my input fields look like when there is an existing value inserted in the database:

    <input
      class="field"
      type="text"
      style="background-color: rgb(255, 255, 255);"
      value=""
      name="input_18748_388_1257894000_46549876"/>


I would like to:

  • Check if the value is already in the database

  • Check if the user want to replace an existing value by nothing or zero and disallow it.

  • Check if the user is trying to insert 0 in a new field and disallow it.


Solved:

  $("input[name^='input_"+var+"_']")
                .each(function() {
                    if ($(this).attr('name').match(/^input_\d+_\d+_\d+_\d+/)
                        && ($(this).val() == '' || $(this).val() <= 0))
                    {
                        displayDialog("<?=_('error')?>")
                        flag_error = 1;
                        return false;
                    }
      });
    // Submit the form.
A: 

Add a class "my_very_special_input" to every input element you would like to validate that way:

$(".my_very_special_input").change( function() {
                            if ($(this).val() <= 0) {
                                    alert('You cant delete this');
                                    flag_error = 1;
                                    return false;
                              }
                    });
antpaw
Thats a good way to do it but unfortunately, I need to check if the name field is ending by '_' or by '_46549876' in order to know if there is already something for this field in the db. I could also match them all with $("input[name^='input_"+var+"_']")
mnml
i think you are confusing the name and the value attr, the normal user cant change the value of the name attr. and thats why you can decide the add the class
antpaw
+1  A: 

Try this:

$('input[name]')
 .filter (function () { 
  return $(this).attr('name').match (/^input_\d+_\d+_\d+_.+/$); 
 })
 .each (function () {
  if ($(this).val() <= 0) {
   //... raise a fuss ...
  }
 });

This needs to be called on submit or change as you prefer.

K Prime
+1  A: 

You need to bind a keydown event and check keycodes. This example will disable space and the 0 char in input fields with the name attribute ending with '_':

$('input[name$=_]').bind('keydown', function(e) {
    var key = e.keyCode || e.which;
    if (key == 48 || key == 32) {
        e.preventDefault();
    }
})
David
A: 

You may benefit the Regex Selector for jQuery Plugin. It allows code like:

$('div:regex(input,^input_(\d+|)+_?$)'); //just an example regex

Also, note that you can combine multiple attribute selectors, ie:

$("input[name^='input_'][name$='_']")
Kobi
A: 
 $("input[name^='input_"+var+"_']")
                .each(function() {
                    if ($(this).attr('name').match(/^input_\d+_\d+_\d+_\d+/)
                        && ($(this).val() == '' || $(this).val() <= 0))
                    {
                        displayDialog("<?=_('error')?>")
                        flag_error = 1;
                        return false;
                    }
                });
mnml