Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html5 input type required without a form. Does it work?

I want to use: HTML 5 Required.

such as

<input type='text' required>

it only seems to work if the input type is in a form. Can I not use it on its own and then by javascript call some sort of validate first method ?

More comments following feedback:

I have

    <input id='name' type='text' required>
    <input id='surname' type='text' required>
    <input id='send' type='button' onclick ='send()'>

    function send() {
       if (document.getElementById('name').value == '') { alert('missing name'); return }
       if (document.getElementById('surname').value == '') { alert('missing surname'); return }

       // logic now...

    }

The bit where it checks the input params and sends alert that's the bit I would like to change by using HTML 5

like image 539
Zo72 Avatar asked Oct 12 '11 13:10

Zo72


People also ask

Does required work without form?

With 'required', this should work only if form is submitted.

Can I use input tag without form?

Yes, you can have a valid input without a form.

Can we use required attribute without form?

Does it work? Yes, but without a form around them the browser can't see the connection between them...

Are HTML forms necessary?

The HTML form tag is required when you want to collect information that visitors provide. For example, you may want to collect specific data from visitors, such as name, email address, and password. The HTML <form> tag is used to create a form.


1 Answers

Sure, use

document.getElementById('your_input_id').validity.valid

to check validity of field dynamically.

like image 98
RReverser Avatar answered Sep 18 '22 16:09

RReverser