Hướng dẫn validation in html5 w3schools
JavaScript FormsHTML form validation can be done by JavaScript. Show
If a form field (fname) is empty, this function alerts a message, and returns false, to prevent the form from being submitted: JavaScript Example function validateForm() { The function can be called when the form is submitted: HTML Form ExampleTry it Yourself » JavaScript Can Validate Numeric InputJavaScript is often used to validate numeric input: Automatic HTML Form ValidationHTML form validation can be performed automatically by the browser: If a form field (fname) is empty, the HTML Form ExampleTry it Yourself » Automatic HTML form validation does not work in Internet Explorer 9 or earlier. Data ValidationData validation is the process of ensuring that user input is clean, correct, and useful. Typical validation tasks are:
Most often, the purpose of data validation is to ensure correct user input. Validation can be defined by many different methods, and deployed in many different ways. Server side validation is performed by a web server, after input has been sent to the server. Client side validation is performed by a web browser, before input is sent to a web server. HTML Constraint ValidationHTML5 introduced a new HTML validation concept called constraint validation. HTML constraint validation is based on:
Constraint Validation HTML Input Attributes
For a full list, go to HTML Input Attributes. Constraint Validation CSS Pseudo Selectors
For a full list, go to CSS Pseudo Classes. This chapter describes the different attributes for the HTML The value AttributeThe input ExampleInput fields with initial (default) values: Try it Yourself » The readonly AttributeThe input A read-only input field cannot be modified (however, a user can tab to it, highlight it, and copy the text from it). The value of a read-only input field will be sent when submitting the form! ExampleA read-only input field: Try it Yourself » The disabled AttributeThe input A disabled input field is unusable and un-clickable. The value of a disabled input field will not be sent when submitting the form! ExampleA disabled input field: Try it Yourself » The size AttributeThe input
The default value for Note: The ExampleSet a width for an input field: Try it Yourself » The maxlength AttributeThe input Note: When a ExampleSet a maximum length for an input field: Try it Yourself » The min and max AttributesThe input The Tip: Use the max and min attributes together to create a range of legal values. ExampleSet a max date, a min date, and a range of legal values: Try it Yourself » The multiple AttributeThe input The ExampleA file upload field that accepts multiple values: Try it Yourself » The pattern AttributeThe input The Tip: Use the global title attribute to describe the pattern to help the user. Tip: Learn more about regular expressions in our JavaScript tutorial. ExampleAn input field that can contain only three letters (no numbers or special characters): Try it Yourself » The placeholder AttributeThe input The short hint is displayed in the input field before the user enters a value. The ExampleAn input field with a placeholder text: Try it Yourself » The required AttributeThe input The ExampleA required input field: Try it Yourself » The step AttributeThe input Example: if step="3", legal numbers could be -3, 0, 3, 6, etc. Tip: This attribute can be used together with the max and min attributes to create a range of legal values. The ExampleAn input field with a specified legal number intervals: Try it Yourself » Note: Input restrictions are not foolproof, and JavaScript provides many ways to add illegal input. To safely restrict input, it must also be checked by the receiver (the server)! The autofocus AttributeThe input ExampleLet the "First name" input field automatically get focus when the page loads: Try it Yourself » The height and width AttributesThe input Tip: Always specify both the height and width attributes for images. If height and width are set, the space required for the image is reserved when the page is loaded. Without these attributes, the browser does not know the size of the image, and cannot reserve the appropriate space to it. The effect will be that the page layout will change during loading (while the images load). ExampleDefine an image as the submit button, with height and width attributes: Try it Yourself » The list AttributeThe input ExampleAn element with pre-defined values in a Try it Yourself » The autocomplete AttributeThe input Autocomplete allows the browser to predict the value. When a user starts to type in a field, the browser should display options to fill in the field, based on earlier typed values. The ExampleAn HTML form with autocomplete on, and off for one input field: Try it Yourself » Tip: In some browsers you may need to activate an autocomplete function for this to work (Look under "Preferences" in the browser's menu). HTML ExercisesTest Yourself With ExercisesExercise:In the input field below, add placeholder that says "Your name here". Start the Exercise HTML Form and Input Elements
For a complete list of all available HTML tags, visit our HTML Tag Reference. |