Forms Support

The Input supports a native KendoReact implementation of HTML5 form validation.

The forms supports enables you to validate input values and prevent the submission of forms which are in an invalid state.

In addition to the exposed form support settings, the Input provides the same properties and behaves in the same way as a native HTML input element.

Functionality and Features

Setting Validation Requirements

The Input supports props for setting basic validation requirements—for example, the minimum and maximum length. The minLength, maxLength, and required properties enable the user to pre-define minimum requirements which define the validity state of the component and allow a successful form submission.

import * as React from 'react';
import * as ReactDOM from 'react-dom';

import { Input } from '@progress/kendo-react-inputs';

class App extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            success: false
        };
    }
    render() {
        return (
            <div className="row example-wrapper">
                <div className="col-xs-12 col-sm-6 offset-sm-3 example-col">
                    <div className="card">
                        <div className="card-block">
                            <form className="k-form" onSubmit={this.handleSubmit}>
                                <fieldset>
                                    <legend>Create new account:</legend>
                                    <div className="mb-3">
                                        <Input
                                            name="username"
                                            style={{ width: "100%" }}
                                            label="First Name"
                                            pattern={"[A-Za-z]+"}
                                            minLength={2}
                                            required={true}
                                        />
                                    </div>
                                    <div className="mb-3">
                                        <Input
                                            name="password"
                                            type="password"
                                            style={{ width: '100%' }}
                                            label="Password"
                                            required={true}
                                            minLength={6}
                                            maxLength={18}
                                        />
                                    </div>
                                    <div className="mb-3">
                                        <Input
                                            name="confirmPassword"
                                            type="password"
                                            style={{ width: '100%' }}
                                            label="Confirm Password"
                                            minLength={6}
                                            maxLength={18}
                                        />
                                    </div>
                                    <div className="mb-3">
                                        <Input
                                            name="email"
                                            type="email"
                                            style={{ width: '100%' }}
                                            label="Email address"
                                            required={true}
                                        />
                                    </div>
                                </fieldset>
                                <input type="submit" className="k-button k-primary" value="Search" />
                            </form>
                        </div>
                    </div>
                </div>
                {this.state.success && (
                    <div
                        className="alert alert-success"
                        style={{ position: 'absolute' }}
                    >
                        Form submitted!
                </div>)}
            </div>
        );
    }

    handleSubmit = (event) => {
        event.preventDefault();
        this.setState({ success: true });
        setTimeout(() => { this.setState({ success: false }); }, 3000);
    }
}

ReactDOM.render(
    <App />,
    document.querySelector('my-app')
);

Setting Custom Validation Messages

You can implement custom error messages which will be displayed when the Input is in an invalid state and the user tries to submit the form. To render a custom validation message, set the validationMessage prop.

import * as React from 'react';
import * as ReactDOM from 'react-dom';

import { Input } from '@progress/kendo-react-inputs';

class App extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            success: false
        };
    }
    render() {
        const usernameValidationMessage = "Your username should contain only letters!";
        const passwordValidationMessage = "Please enter password between 6 and 16 characters!";

        return (
            <div className="row example-wrapper">
                <div className="col-xs-12 col-sm-6 offset-sm-3 example-col">
                    <div className="card">
                        <div className="card-block">
                            <form className="k-form" onSubmit={this.handleSubmit}>
                                <fieldset>
                                    <legend>Create new account:</legend>
                                    <div className="mb-3">
                                        <Input
                                            name="username"
                                            style={{ width: "100%" }}
                                            label="First Name"
                                            pattern={"[A-Za-z]+"}
                                            minLength={2}
                                            required={true}
                                            validationMessage={usernameValidationMessage}
                                        />
                                    </div>
                                    <div className="mb-3">
                                        <Input
                                            name="password"
                                            type="password"
                                            style={{ width: '100%' }}
                                            label="Password"
                                            required={true}
                                            minLength={6}
                                            maxLength={18}
                                            validationMessage={passwordValidationMessage}
                                        />
                                    </div>
                                    <div className="mb-3">
                                        <Input
                                            name="confirmPassword"
                                            type="password"
                                            style={{ width: '100%' }}
                                            label="Confirm Password"
                                            required={true}
                                            minLength={6}
                                            maxLength={18}
                                            validationMessage={passwordValidationMessage}
                                        />
                                    </div>
                                    <div className="mb-3">
                                        <Input
                                            name="email"
                                            type="email"
                                            style={{ width: '100%' }}
                                            label="Email address"
                                            required={true}
                                        />
                                    </div>
                                </fieldset>
                                <input type="submit" className="k-button k-primary" value="Search" />
                            </form>
                        </div>
                    </div>
                </div>
                {this.state.success && (
                    <div
                        className="alert alert-success"
                        style={{ position: 'absolute' }}
                    >
                        Form submitted!
                </div>)}
            </div>
        );
    }

    handleSubmit = (event) => {
        event.preventDefault();
        this.setState({ success: true });
        setTimeout(() => { this.setState({ success: false }); }, 3000);
    }
}

ReactDOM.render(
    <App />,
    document.querySelector('my-app')
);

Enforcing the Validity State

The Input enables you to set its validity state by utilizing its props. Setting the validity state allows for the implementation of complex validation scenarios that are based on multiple sources. To override the current validity state and apply the corresponding styles, use the valid prop.

import * as React from 'react';
import * as ReactDOM from 'react-dom';

import { Input } from '@progress/kendo-react-inputs';

class App extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            password: '',
            confirmPassword: '',
            success: false
        };
    }
    render() {
        const usernameValidationMessage = "Your username should contain only letters!";
        const passwordValidationMessage = "Please enter password between 6 and 16 characters!";
        const confirmPasswordValidationMessage = "Passwords does not match!";

        return (
            <div className="row example-wrapper">
                <div className="col-xs-12 col-sm-6 offset-sm-3 example-col">
                    <div className="card">
                        <div className="card-block">
                            <form className="k-form" onSubmit={this.handleSubmit}>
                                <fieldset>
                                    <legend>Create new account:</legend>
                                    <div className="mb-3">
                                        <Input
                                            name="username"
                                            style={{ width: "100%" }}
                                            label="First Name"
                                            pattern={"[A-Za-z]+"}
                                            minLength={2}
                                            required={true}
                                            validationMessage={usernameValidationMessage}
                                        />
                                    </div>
                                    <div className="mb-3">
                                        <Input
                                            value={this.state.password}
                                            onChange={this.handleChange}
                                            name="password"
                                            type="password"
                                            style={{ width: '100%' }}
                                            label="Password"
                                            required={true}
                                            minLength={6}
                                            maxLength={18}
                                            validationMessage={passwordValidationMessage}
                                        />
                                    </div>
                                    <div className="mb-3">
                                        <Input
                                            value={this.state.confirmPassword}
                                            onChange={this.handleChange}
                                            name="confirmPassword"
                                            type="password"
                                            style={{ width: '100%' }}
                                            label="Confirm Password"
                                            valid={this.state.password && this.state.password === this.state.confirmPassword}
                                            minLength={6}
                                            maxLength={18}
                                            validationMessage={confirmPasswordValidationMessage}
                                        />
                                    </div>
                                    <div className="mb-3">
                                        <Input
                                            name="email"
                                            type="email"
                                            style={{ width: '100%' }}
                                            label="Email address"
                                            required={true}
                                        />
                                    </div>
                                </fieldset>
                                <input type="submit" className="k-button k-primary" value="Search" />
                            </form>
                        </div>
                    </div>
                </div>
                {this.state.success && (
                    <div
                        className="alert alert-success"
                        style={{ position: 'absolute' }}
                    >
                        Form submitted!
                </div>)}
            </div>
        );
    }

    handleChange = (event) => {
        this.setState({
            [event.target.name]: event.target.value
        });
    }

    handleSubmit = (event) => {
        event.preventDefault();
        this.setState({ success: true });
        setTimeout(() => { this.setState({ success: false }); }, 3000);
    }
}

ReactDOM.render(
    <App />,
    document.querySelector('my-app')
);

Disabling the Validation Styling

By default, to achieve a visual representation of its invalid state, the Input applies the k-state-invalid class name to the wrapping component. To disable the utilization of k-state-invalid, set the validityStyles property to false.

import * as React from 'react';
import * as ReactDOM from 'react-dom';

import { Input } from '@progress/kendo-react-inputs';

class App extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            password: '',
            confirmPassword: '',
            success: false
        };
    }
    render() {
        const usernameValidationMessage = "Your username should contain only letters!";
        const passwordValidationMessage = "Please enter password between 6 and 16 characters!";
        const confirmPasswordValidationMessage = "Passwords does not match!";

        return (
            <div className="row example-wrapper">
                <div className="col-xs-12 col-sm-6 offset-sm-3 example-col">
                    <div className="card">
                        <div className="card-block">
                            <form className="k-form" onSubmit={this.handleSubmit}>
                                <fieldset>
                                    <legend>Create new account:</legend>
                                    <div className="mb-3">
                                        <Input
                                            validityStyles={false}
                                            name="username"
                                            style={{ width: "100%" }}
                                            label="First Name"
                                            pattern={"[A-Za-z]+"}
                                            minLength={2}
                                            required={true}
                                            validationMessage={usernameValidationMessage}
                                        />
                                    </div>
                                    <div className="mb-3">
                                        <Input
                                            validityStyles={false}
                                            value={this.state.password}
                                            onChange={this.handleChange}
                                            name="password"
                                            type="password"
                                            style={{ width: '100%' }}
                                            label="Password"
                                            required={true}
                                            minLength={6}
                                            maxLength={18}
                                            validationMessage={passwordValidationMessage}
                                        />
                                    </div>
                                    <div className="mb-3">
                                        <Input
                                            validityStyles={false}
                                            value={this.state.confirmPassword}
                                            onChange={this.handleChange}
                                            name="confirmPassword"
                                            type="password"
                                            style={{ width: '100%' }}
                                            label="Confirm Password"
                                            valid={this.state.password && this.state.password === this.state.confirmPassword}
                                            minLength={6}
                                            maxLength={18}
                                            validationMessage={confirmPasswordValidationMessage}
                                        />
                                    </div>
                                    <div className="mb-3">
                                        <Input
                                            validityStyles={false}
                                            name="email"
                                            type="email"
                                            style={{ width: '100%' }}
                                            label="Email address"
                                            required={true}
                                        />
                                    </div>
                                </fieldset>
                                <input type="submit" className="k-button k-primary" value="Search" />
                            </form>
                        </div>
                    </div>
                </div>
                {this.state.success && (
                    <div
                        className="alert alert-success"
                        style={{ position: 'absolute' }}
                    >
                        Form submitted!
                </div>)}
            </div>
        );
    }

    handleChange = (event) => {
        this.setState({
            [event.target.name]: event.target.value
        });
    }

    handleSubmit = (event) => {
        event.preventDefault();
        this.setState({ success: true });
        setTimeout(() => { this.setState({ success: false }); }, 3000);
    }
}

ReactDOM.render(
    <App />,
    document.querySelector('my-app')
);

In this article