Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade Teachers Spaces Bootcamps Get Certified Upgrade Teachers Spaces Bootcamps
   ❮     
     ❯   

Basic JavaScript

JS TutorialJS SyntaxJS VariablesJS OperatorsJS If ConditionsJS LoopsJS StringsJS NumbersJS FunctionsJS ObjectsJS ScopeJS DatesJS Temporal DatesJS ArraysJS SetsJS MapsJS IterationsJS MathJS RegExpJS DestructuringJS Data TypesJS ErrorsJS DebuggingJS ConventionsJS ReferencesJS 2026JS Versions

JS HTML

JS HTML DOMJS EventsJS ProjectsNew

JS Advanced

JS FunctionsJS ObjectsJS ClassesJS AsynchronousJS ModulesJS Meta & ProxyJS Typed ArraysJS DOM NavigationJS WindowsJS Web APIsJS AJAXJS JSONJS jQueryJS GraphicsJS ExamplesJS Reference


HTML Form Validation


JavaScript Form Validation

HTML form validation can be done by JavaScript.

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() {
  letx = document.forms["myForm"]["fname"].value;
 if (x == "") {
    alert("Name must be filled out");
    return false;
  }
}

The function can be called when the form is submitted:

HTML Form Example

<form name="myForm" action="/action_page.php"onsubmit="return validateForm()" method="post">
Name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
Try it Yourself »

JavaScript Can Validate Numeric Input

JavaScript is often used to validate numeric input:

Please input a number between 1 and 10

Try it Yourself »


Automatic HTML Form Validation

HTML form validation can be performed automatically by the browser:

If a form field (fname) is empty, therequired attribute prevents this form from being submitted:

HTML Form Example

<form action="/action_page.php" method="post">
  <input type="text" name="fname"required>
 <input type="submit" value="Submit">
</form>
Try it Yourself »

Automatic HTML form validation does not work in Internet Explorer 9 or earlier.


Data Validation

Data validation is the process of ensuring that user input is clean, correct, and useful.

Typical validation tasks are:

  • has the user filled in all required fields?
  • has the user entered a valid date?
  • has the user entered text in a numeric field?

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 Validation

HTML5 introduced a new HTML validation concept calledconstraint validation.

HTML constraint validation is based on:

  • Constraint validationHTMLInput Attributes
  • Constraint validationCSS Pseudo Selectors
  • Constraint validationDOM Properties and Methods

Constraint Validation HTML Input Attributes

AttributeDescription
disabledSpecifies that the input element should be disabled
maxSpecifies the maximum value of an input element
minSpecifies the minimum value of an input element
patternSpecifies the value pattern of an input element
requiredSpecifies that the input field requires an element
type Specifies the type of an input element

For a full list, go toHTML Input Attributes.


Constraint Validation CSS Pseudo Selectors

SelectorDescription
:disabledSelects input elements with the "disabled" attribute specified
:invalidSelects input elements with invalid values
:optionalSelects input elements with no "required" attribute specified
:requiredSelects input elements with the "required" attribute specified
:validSelects input elements with valid values

For a full list, go toCSS Pseudo Classes.




×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted ourterms of use,cookies andprivacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.

-->
[8]ページ先頭

©2009-2026 Movatter.jp