Forms Support

The NumericTextBox 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.

Functionality and Features

Setting Validation Requirements

The NumericTextBox supports props for setting basic validation requirements—for example, the minimum and maximum value. The min, max, 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 { NumericTextBox, 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>Please fill in the fields:</legend>
                                    <div className="mb-3">
                                        <Input
                                            name="firstName"
                                            style={{ width: "100%" }}
                                            label="First Name"
                                            pattern={"[A-Za-z]+"}
                                            minLength={2}
                                            required={true}
                                        />
                                    </div>
                                    <div className="mb-3">
                                        <Input
                                            name="lastName"
                                            style={{ width: '100%' }}
                                            label="Last Name"
                                            pattern={"[A-Za-z]+"}
                                            minLength={2}
                                            required={true}
                                        />
                                    </div>
                                    <div>
                                        <NumericTextBox
                                            name="age"
                                            width="100%"
                                            label="Age"
                                            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 NumericTextBox 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 { NumericTextBox, Input } from '@progress/kendo-react-inputs';

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

        this.state = {
            success: false
        };
    }
    render() {
        const firstNameValidationMessage = "Please enter your first name!";
        const lastNameValidationMessage = "Please enter your first name!";
        const ageValidationMessage = "Please enter your age!";

        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>Please fill in the fields:</legend>
                                    <div className="mb-3">
                                        <Input
                                            name="firstName"
                                            style={{ width: "100%" }}
                                            label="First Name"
                                            required={true}
                                            pattern={"[A-Za-z]+"}
                                            minLength={2}
                                            validationMessage={firstNameValidationMessage}
                                        />
                                    </div>
                                    <div className="mb-3">
                                        <Input
                                            name="lastName"
                                            style={{ width: '100%' }}
                                            label="Last Name"
                                            required={true}
                                            pattern={"[A-Za-z]+"}
                                            minLength={2}
                                            validationMessage={lastNameValidationMessage}
                                        />
                                    </div>
                                    <div>
                                        <NumericTextBox
                                            name="age"
                                            width="100%"
                                            label="Age"
                                            required={true}
                                            validationMessage={ageValidationMessage}
                                        />
                                    </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 NumericTextBox 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 { NumericTextBox, Input } from '@progress/kendo-react-inputs';

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

        this.state = {
            firstName: '',
            lastName: '',
            age: null,
            success: false
        };
    }
    render() {
        const ageValidationMessage = "You must be over 18 years old!";

        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>Please fill in the fields:</legend>
                                    <div className="mb-3">
                                        <Input
                                            value={this.state.firstName}
                                            onChange={this.handleChange}
                                            name="firstName"
                                            style={{ width: "100%" }}
                                            label="First Name"
                                            pattern="[A-Za-z]{1,32}"
                                        />
                                    </div>
                                    <div className="mb-3">
                                        <Input
                                            value={this.state.lastName}
                                            onChange={this.handleChange}
                                            name="lastName"
                                            style={{ width: '100%' }}
                                            label="Last Name"
                                        />
                                    </div>
                                    <div>
                                        <NumericTextBox
                                            value={this.state.age}
                                            onChange={this.handleChange}
                                            name="age"
                                            width="100%"
                                            label="Age"
                                            required={true}
                                            valid={this.state.age >= 18}
                                            validationMessage={this.state.age !== null
                                                ? ageValidationMessage
                                                : 'Please enter your age!'}
                                        />
                                    </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 NumericTextBox 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 { NumericTextBox, Input } from '@progress/kendo-react-inputs';

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

        this.state = {
            firstName: '',
            lastName: '',
            age: null,
            success: false
        };
    }
    render() {
        const ageValidationMessage = "You must be over 18 years old!";

        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>Please fill in the fields:</legend>
                                    <div className="mb-3">
                                        <Input
                                            value={this.state.firstName}
                                            onChange={this.handleChange}
                                            name="firstName"
                                            style={{ width: "100%" }}
                                            label="First Name"
                                            pattern="[A-Za-z]{1,32}"
                                        />
                                    </div>
                                    <div className="mb-3">
                                        <Input
                                            value={this.state.lastName}
                                            onChange={this.handleChange}
                                            name="lastName"
                                            style={{ width: '100%' }}
                                            label="Last Name"
                                        />
                                    </div>
                                    <div>
                                        <NumericTextBox
                                            validityStyles={false}

                                            value={this.state.age}
                                            onChange={this.handleChange}
                                            name="age"
                                            width="100%"
                                            label="Age"
                                            required={true}
                                            valid={this.state.age >= 18}
                                            validationMessage={this.state.age !== null
                                                ? ageValidationMessage
                                                : 'Please enter your age!'}
                                        />
                                    </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