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

3 Answers 3

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>

Sign up to request clarification or add additional context in comments.

5 Comments

same as Code Lღver comment : I've read this post, and its showing what you say, but I need to seperate form tag for each input and button
No you don't, trust me ^^ PS: It isn't the same comment, he added a useless attribute to the input
my input field with separate form tag, and my input field in 1 form tag
I'm just modify the code that already created, so I won't learn css, and I'm not front-end dev xD
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
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/

11 Comments

would be hard if I have more than1 input textfield, :(
what would be hard ? You can't modify the first form ?
actually I've 6 inputfield and 1 button for each form tag, so I cant modify your code for all of my inputfield, it's gonna be to long,
could you give me the whole html in a jsfiddle please ?
this is the whole inputfield required i've create jsfiddle.net/9wbranL3/1
|
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.

1 Comment

I've read this post, and its showing what you say, but I need to seperate form tag for each input and button

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.