Validation on input text fields in html - html

Suppose:
There's
First Name : (box for input first name)
Requirement: i cannot left this field empty,error message should generate if i left it empty.
Solve this only through HTML

<form>
first name <input type="text" name="firstname" required>
<input type="submit">
</form>

<input> has a required attribute, as such:
<input type="text" name="username" required>
This will raise an error if the user tries to submit the form without first filling out the input. That being said, please google this next time.
W3Schools Page

Related

Is there a way to have a form with a fixed value that is still visible?

Is there a way to have a form that holds a pre set value (numbers or letters) that cant be changed by the user and isnt hidden to them. I need to so when someone clicks on a product the product id and price is auto filled out, they only need to type in the quantity.
I think you can set the value on the HTML form <input> using the value attribute as always and disable the input by using the disabled html attribute.
Something like this:
<form action="/action_page.php">
First name:
<input type="text" name="fname" value="John" disabled ><br>
Last name:
<input type="text" name="lname" value="Doe"><br>
<input type="submit" value="Submit form">
</form>
the value will be there and the field will be disabled.
Hope it helps,

HTML: Autofill form in bootstrap modal

In most HTML forms when I start typing something (say birth date) navigators propose to sit with previous similar entries. For instance on html form submit the second visit offers me the first visit entries.
However, when using a bootstrap modal containing a form, the same does not happen, for instance: with a form inside.
I do not want to use jquery autocomplete since I do not have a list of potential answers, I just want to have the same behavior in and outside modals.
Thanks.
Browser autofills are notoriously unpredictable - they make educated guesses about the data based on the name attribute of inputs. It's unlikely you'll be able to get this behavior consistently cross-browser.
can you try this :
add the attribute autocomplete = "on" on your form,
maybe it will do the job.
<form action="demo_form.asp" autocomplete="on">
First name:<input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
E-mail: <input type="email" name="email" autocomplete="off"><br>
<input type="submit">
</form>
source: http://www.w3schools.com/tags/att_input_autocomplete.asp
Read through this article it should help get things working for you.
Example:
<input type="text" id="name" name="name" autocomplete="name">
<input type="tel" id="tel" name="tel" autocomplete="home tel">

Form submission minor issue

I have a form like this. I want to know that will the form submission work if the is placed in the middle of the text fields?
For example:
<input type="text" name="fname"> // First Name
<input type="text" name="lname"> // Last Name
<form method="post" action=""> // Post
<input type="text" name="username"> // Username
<input type="text" name="password"> // Password
<input type="submit" value="Submit"> // Submit Button
Will the submission work for First Name and Last Name field as the is after them so they do not come inside the form.
Your form elements (like your input boxes) have to be between an opening <form> and a closing </form> tag. So your fname and lname will be ignored. (Your closing </form> is missing, too.)
Why do you have to add your form elements between form tags? This allows you to add multiple forms to one page. To identify which element contains to which form, they have to be between the form tags.
Example "Login & register on the same site":
<form method="POST" action="login.php">
User: <input type="text" name="username">
Password: <input type="password" name="password">
<input type="submit" value="Login">
</form>
<form method="POST" action="register.php">
Mail: <input type="text" name="email">
User: <input type="username" name="username">
Password: <input type="password" name="password">
Repeat password: <input type="password" name="pwdagain">
<input type="submit" value="Register">
</form>
Refer this site for further information: http://www.w3schools.com/html/html_forms.asp
First name and last name will not post. Username and password will post but u have to close form tag first.
If you transform you code like this it will post all :
<input type="text" name="fname" form="my_form_id"> // First Name
<input type="text" name="lname" form="my_form_id"> // Last Name
<form id="my_form_id" method="post" action="#"> // Post
<input type="text" name="username"> // Username
<input type="text" name="password"> // Password
<input type="submit" value="Submit"> // Submit Button
</form>
#shubham-jha If you want multiple submit buttons under a single form, you may use AJAX.
Create a JavaScript function on click, decide to which URL you want to send this data and then change form action using jQuery, then submit using JavaScript.
Jquery to change form action
There is some news on this front, it seems.
MDN has this for you to review
form HTML5
"The form element that the input element is associated with (its form owner). The value of the attribute must be an id of an element in the same document. If this attribute is not specified, this element must be a descendant of an element. This attribute enables you to place elements anywhere within a document, not just as descendants of their form elements."
Perhaps you can still achieve what you wished for. Only question then, is what browser support you must have.

Registration form and removing all fields

When someone do not fill all fields in my formular the site is showing error and it is refreshing so that someone must fill it again.
How to make the site remember correctly filled fields?
When the form is submitted, then on page refresh the $_POST data is still intact. So what you can do is something like this:
<input type="text" name="field_name" value="<?php echo (isset($_POST['field_name']) ? $_POST['field_name'] : ''); ?>">
What it does is that if the $_POST data for the field field_name is set, then echo its value.
Use of autocomplete can preserve your previous inputs.
Autocomplete ON will preserve the value and OFF will prevent the input box from remembering. In sample code, email have off as autocomplete.
<input autocomplete="on|off">
OR
<form action="/action" autocomplete="on">
First name:<input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
E-mail: <input type="email" name="email" autocomplete="off"><br>
<input type="submit">
</form>

HTML5 required Tag. How to make form valid again?

I am using the html5 attribute required to valid my input. I find once the form has become invalid, it doesn't recognize when the user has typed in valid information.
For example:
<input id="name" type="text" name="username" required oninvalid="setCustomValidity('Please enter a username!')">
If the user misses this feild it highlight red and tells the user to fill it in. If they then fill it in and click submit, it tells the user to fill it in.
How do I recover my forms once they have become invalid?
It's not rechecking after declaring the form element invalid. Try adding an empty setCustomValidity string to oninput. Ie:
<input id="name" type="text" name="username" required oninvalid="setCustomValidity('Please enter a username!')" oninput="setCustomValidity('')" >
I do something similar to #Julie answer but use onchange instead of oninput ...
I use oninvalid to set the custom validty error message and then use onchange to reset the message so the form can submit properly.
<input type="number" oninvalid="this.setCustomValidity('Please enter an INTEGER')" onchange="this.setCustomValidity('')" name="integer-only" value="0" min="0" step="1">