1
var submitbutton = $('#form_submit');
submitbutton.addClass('disabled');
$('#form_primer').on('keyup', validator(this));
$('#form_szek').on('keyup', validator(this));

function validator(event){
    if(isNaN(event.value)){
        $(event).css('border-color', 'red');
        submitbutton.addClass('disabled');
    }
    else{
        $(event).css('border-color', 'green');
        submitbutton.removeClass('disabled');
    }
  };

So, I have this javascript, and don't want to write the validator function twice. This script should disable the submit button and color the border red or green based on the input. How can I pass the form element to the function? Also, how can I disable the form submission properly? The button can't be pressed, but if the user hits enter with text inside, it will still submit it.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
Drake
  • 111
  • 1
  • 8
  • Avoid using `event` as a variable to refer your element in the event handler, it's dedicated for the event itself. – cнŝdk Apr 13 '17 at 15:47

2 Answers2

2

You don't have to pass a variable to the event handler, you can access element directly using this keyword:

var submitbutton = $('#form_submit');
submitbutton.addClass('disabled');
$('#form_primer').on('keyup', validator);
$('#form_szek').on('keyup', validator);

function validator() {
  if (isNaN(this.value)) {
    $(this).css('border-color', 'red');
    submitbutton.addClass('disabled');
  } else {
    $(this).css('border-color', 'green');
    submitbutton.removeClass('disabled');
  }
};

Note:

Avoid using event as a variable to refer your element in the event handler, it's dedicated for the event itself.

Demo:

This is a working Demo:

var submitbutton = $('#form_submit');
submitbutton.addClass('disabled');
$('#form_primer').on('keyup', validator);
$('#form_szek').on('keyup', validator);

function validator() {
  if (isNaN(this.value)) {
    $(this).css('border-color', 'red');
    submitbutton.addClass('disabled');
  } else {
    $(this).css('border-color', 'green');
    submitbutton.removeClass('disabled');
  }
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="submit" id="form_submit">
<input type="text" id="form_primer">
<input type="text" id="form_szek">
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
  • @MyiEyeNo in fact it's, you can check it in the Demo. – cнŝdk Apr 13 '17 at 15:55
  • Got it, its working, thanks ^^ Also, my other question: how do I stop a form from submiting on enter pressed? – Drake Apr 13 '17 at 15:55
  • Sorry, I was thinking that THIS was supposed to be the outside context. My bad. – MyiEye Apr 13 '17 at 15:56
  • @Drake you need to trgger `keyup` event on the `form` and use `e.preventDefault()` if Enter is pressed, take a look at : http://stackoverflow.com/questions/11235622/jquery-disable-form-submit-on-enter. – cнŝdk Apr 13 '17 at 15:58
0

Your function validator should be in function() {} of event.

Edit

Changed event var to object. Good point there @evolutionxbox

var submitbutton = $('#form_submit');
$('#form_primer').on('keyup', function() {
  validator(this)
});
$('#form_szek').on('keyup', function() {
  validator(this)
});

function validator(object) {
  if (isNaN(object.value)) {
    $(object).css('border-color', 'red');
    submitbutton.addClass('disabled');
  } else {
    $(object).css('border-color', 'green');
    submitbutton.removeClass('disabled');
  }
};
.disabled {
  border: 0;
  background-color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="submit" id="form_submit">
<input type="text" id="form_primer">
<input type="text" id="form_szek">
Community
  • 1
  • 1
Oen44
  • 3,156
  • 1
  • 22
  • 31