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
With 'required', this should work only if form is submitted.
Yes, you can have a valid input without a form.
Does it work? Yes, but without a form around them the browser can't see the connection between them...
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.
Sure, use
document.getElementById('your_input_id').validity.valid
to check validity of field dynamically.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With