HTML form Attribute

HTML form Attribute Tutorials

1. HTML <form> element attributes

In HTML there are various attributes available for <form> element which are given below:

2. HTML action attribute

The action attribute of <form> element defines the process to be performed on form when form is submitted, or it is a URI to process the form information.

The action attribute value defines the web page where information proceed. It can be .php, .jsp, .asp, etc. or any URL where you want to process your form.

3. Example:

  1. <form action="action.html" method="post">  
  2. <label>User Name:</label><br>  
  3. <input type="text" name="name"><br><br>  
  4. <label>User Password</label><br>  
  5. <input type="password" name="pass"><br><br>  
  6.  <input type="submit">  
  7.    </form>  

4. Output:

It will redirect to a new page "action.html" when you click on submit button

5. HTML method attribute

The method attribute defines the HTTP method which browser used to submit the form. The possible values of method attribute can be:

  • post: We can use the post value of method attribute when we want to process the sensitive data as it does not display the submitted data in URL.

6. Example:

<form action="action.html" method="post">  

get: The get value of the method attribute is default value while submitting the form. But this is not secure as it displays data in URL after submitting the form.

7. Example:

<form action="action.html" method="get"> 

When submitting the data, it will display the entered data in the form of:


8. HTML target attribute

The target attribute defines where to open the response after submitting the form. The following are the keywords used with the target attribute.

  • _self: If we use _self as an attribute value, then the response will display in current page only.

9. Example:

<form action="action.html" method="get" target="_self">

_blank: If we use _blank as an attribute it will load the response in a new page.

10. Example:

<form action="action.html" method="get" target="_blank">  

11. HTML autocomplete attribute

The HTML autocomplete attribute is a newly added attribute of HTML5 which enables an input field to complete automatically. It can have two values "on" and "off" which enables autocomplete either ON or OFF. The default value of autocomplete attribute is "on".

12. Example:

<form action="action.html" method="get" autocomplete="on">  

13. Example:

<form action="action.html" method="get" autocomplete="off">  

14. HTML enctype attribute

The HTML enctype attribute defines the encoding type of form-content while submitting the form to the server. The possible values of enctype can be:

  • application/x-www-form-urlencoded: It is default encoding type if the enctype attribute is not included in the form. All characters are encoded before submitting the form.

15. Example:

<form action="action.html" method="post" enctype="application/x-www-form-urlencoded" >  

multipart/form-data: It does not encode any character. It is used when our form contains file-upload controls.

16. Example:

<form action="action.html" method="post" enctype="multipart/form-data">  

  • text/plain (HTML5): In this encoding type only space are encoded into + symbol and no any other special character encoded.

17. Example:

<form action="action.html" method="post" enctype="text/plain" >  

18. HTML novalidate attribute HTML5

The novalidate attribute is newly added Boolean attribute of HTML5. If we apply this attribute in form then it does not perform any type of validation and submit the form.

19. Example:

<form action "action.html" method "get" novalidate>  

20. HTML <input> element attribute

21. HTML name attribute

The HTML name attribute defines the name of an input element. The name and value attribute are included in HTTP request when we submit the form.

22. Example:

  1. <form action "action.html" method = "get">  
  2.          Enter name:<br><input type="name" name="uname"><br>  
  3.          Enter age:<br><input type="number" name="age"><br>  
  4.          Enter email:<br><input type="email"><br>  
  5.          <input type="submit" value="Submit">  
  6.       </form>  

23. Output:

24. HTML value attribute

The HTML value attribute defines the initial value or default value of an input field.

25. Example:

  1. <form>  
  2.         <label>Enter your Name</label><br>  
  3.         <input type="text" name="uname" value="Enter Name"><br><br>  
  4.         <label>Enter your Email-address</label><br>  
  5.         <input type="text" name="uname" value="Enter email"><br><br>  
  6.           <label>Enter your password</label><br>  
  7.         <input type="password" name="pass" value=""><br><br>  
  8.         <input type="submit" value="login">  
  9.    </form>   


27. HTML required attribute HTML5

HTML required is a Boolean attribute which specifies that user must fill that filed before submitting the form.

28. Example:

  1. <form>  
  2.         <label>Enter your Email-address</label><br>  
  3.         <input type="text" name="uname" required><br><br>  
  4.          <label>Enter your password</label><br>  
  5.         <input type="password" name="pass"><br><br>  
  6.         <input type="submit" value="login">  
  7.    </form>  

29. HTML autofocus attribute HTML5

The autofocus is a Boolean attribute which enables a field automatically focused when a webpage loads.

30. Example:

Enter your Email-address</label><br>  
type="text" name="uname" autofocus><br><br>  
Enter your password</label><br>  
type="password" name="pass"><br><br>  
type="submit" value="login">  

31. HTML placeholder attribute HTML5

The placeholder attribute specifies a text within an input field which informs the user about the expected input of that filed.

The placeholder attribute can be used with text, password, email, and URL values.

When the user enters the value, the placeholder will be automatically removed.

32. Output:

  1. <form>  
  2.         <label>Enter your name</label><br>  
  3.         <input type="text" name="uname" placeholder="Your name"><br><br>  
  4.             <label>Enter your Email address</label><br>  
  5.         <input type="email" name="email" placeholder=""><br><br>  
  6.             <label>Enter your password</label><br>  
  7.         <input type="password" name="pass" placeholder="your password"><br><br>  
  8.         <input type="submit" value="login">  
  9.     </form>  

33. HTML disabled attribute

The HTML disabled attribute when applied then it disable that input field. The disabled field does not allow the user to interact with that field.

The disabled input filed does not receive click events, and these input value will not be sent to the server when submitting the form.

34. Example:

<input type="text" name="uname" disabled><br><br>  

35. HTML size attribute

The size attribute controls the size of the input field in typed characters.

36. Example:

  1. <label>Account holder name</label><br>  
  2.         <input type="text" name="uname" size="40" required><br><br>  
  3.         <label>Account number</label><br>  
  4.         <input type="text" name="an" size="30" required><br><br>  
  5.         <label>CVV</label><br>  
  6.         <input type="text" name="cvv"  size="1" required><br><br>  

37. HTML form attribute

HTML form attribute allows a user to specify an input filed outside the form but remains the part of the parent form.

38. Example:

  1. User email: <br><input type="email" name="email"  form="fcontrol"  required><br>  
  2.          <input type="submit" form="fcontrol">