2

Is it possible to add required on different form, before execute button?

let say I have a HTML code when I used this code in JS:

$(function() {
  $('#button1').click(function() {
    $("#email").prop('required', true);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <input type="text" id="email" name="email" placeholder="Email*">
</form>
<form>
  <button id="button1">Submit</button>
</form>

the button keep execute even the textfield of email is null,

expected result: need to show this text if input field is null when click the button

required

jsfiddle

F0XS
  • 1,271
  • 3
  • 15
  • 19
flix
  • 1,688
  • 3
  • 34
  • 64

3 Answers3

1

Working fiddle: https://jsfiddle.net/andreitodorut/xe3z0nas/

HTML:

<form id="form1">
  <input type="text" id="email_to" name="email_to" required="required" placeholder="Email*">
  <button class="hide submit-form">
  Submit
  </button>
</form>
<form data-submit="#form1">
  <button id="button1">Submit</button>
</form>

JS:

$(document).ready(function() {
    $('form[data-submit]').submit(function(e){
    e.preventDefault();

    var formToSubmit = $(this).attr('data-submit');

    if(formToSubmit){
        $(formToSubmit).find('.submit-form').trigger('click');
    }
  })
})

CSS:

.hide {
    display: none; 
}

UPDATE https://jsfiddle.net/9wbranL3/4/

Andrei Todorut
  • 4,260
  • 2
  • 17
  • 28
1

You just had to put everything in the same form.

As you can see, now that i've commented the separator, it works ^^

$(function() {
  $('#button1').click(function() {
    $("#email").prop('required', true);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <input type="text" id="email" name="email" placeholder="Email*">
<!--</form>
<form>-->
  <button id="button1">Submit</button>
</form>
Marco Salerno
  • 5,131
  • 2
  • 12
  • 32
  • same as Code Lღver comment : I've read [this post](https://stackoverflow.com/questions/19166685/jquery-add-required-to-input-fields), and its showing what you say, but **I need to seperate form tag** for each input and button – flix Jan 24 '18 at 08:42
  • No you don't, trust me ^^ PS: It isn't the same comment, he added a useless attribute to the input – Marco Salerno Jan 24 '18 at 08:43
  • my input field with [separate form tag](https://pasteboard.co/H4nJOMX.png), and my input field in [1 form tag](https://pasteboard.co/H4nJsb1.png) – flix Jan 24 '18 at 08:50
  • I'm just modify the code that already created, so I won't learn css, and I'm not front-end dev xD – flix Jan 24 '18 at 08:55
  • I'm not a frontend developer too, but if you think that you can use different forms, it means that you aren't a backend developer, cause you didn't understand how does this aarchitecture works – Marco Salerno Jan 24 '18 at 08:57
0

You have to put the button inside the same form tag.

HTML:

<form>
  <input type="text" id="email" name="email" placeholder="Email*">

  <button id="button1">Submit</button>
</form>

And the add the require attribute to email field, code is below:

  <input type="text" id="email" name="email" placeholder="Email*" required="true">

When you will submit the form it will check the blank condition and will show the same message are showing in the image.

Code Lღver
  • 15,573
  • 16
  • 56
  • 75
  • I've read [this post](https://stackoverflow.com/questions/19166685/jquery-add-required-to-input-fields), and its showing what you say, but I need to seperate form tag for each input and button – flix Jan 24 '18 at 08:13