@@ -4,11 +4,100 @@ A customized validatorjs library to validate the react forms. This library is ex
44
55* [ Supported Rules] ( https://www.npmjs.com/package/validatorjs#available-rules ) (It is supports all validatorjs rules)
66* [ Documentation] ( https://gokulakannant.github.io/react-form-input-validation/index.html )
7- * [ Demo] ( https://gokulakannant.github.io/react-form-input-validation/demo/index.html )
87
98## Usage
109
11- [ Click here] ( https://gokulakannant.github.io/react-form-input-validation/classes/reactformvalidator.html ) to see the usage and its example
10+ A example form has given below for basic usages. View all available apis in documentation
11+
12+ ``` js
13+ import React from " react" ;
14+ import ReactFormValidation from " react-form-input-validation" ;
15+
16+ class ValidationForm extends React .Component {
17+ constructor (props ) {
18+ super (props);
19+ this .state = {
20+ fields: {
21+ name: " " ,
22+ email: " " ,
23+ phone_number: " "
24+ },
25+ inputErrors: {}
26+ };
27+ this .form = new ReactFormValidation (
28+ this ,
29+ {
30+ name: " required" ,
31+ email: " required|email" ,
32+ phone_number: " required|numeric|digits_between:10,12" ,
33+ },
34+ (fields ) => {
35+ alert (JSON .stringify (fields));
36+ }
37+ );
38+ }
39+
40+ render () {
41+ return (< React .Fragment >
42+ < form onSubmit= {this .form .handleSubmit }>
43+ < p>
44+ < label>
45+ Name
46+ < input
47+ type= " text"
48+ name= " name"
49+ onBlur= {this .form .handleBlurEvent }
50+ onChange= {this .form .handleFieldsChange }
51+ value= {this .state .fields .name }
52+ / >
53+ < / label>
54+ < label className= " error" >
55+ {this .state .inputErrors .name ? this .state .inputErrors .name .message : " " }
56+ < / label>
57+ < / p>
58+
59+ < p>
60+ < label>
61+ Email
62+ < input
63+ type= " email"
64+ name= " email"
65+ onBlur= {this .form .handleBlurEvent }
66+ onChange= {this .form .handleFieldsChange }
67+ value= {this .state .fields .email }
68+ / >
69+ < / label>
70+ < label className= " error" >
71+ {this .state .inputErrors .email ? this .state .inputErrors .email .message : " " }
72+ < / label>
73+ < / p>
74+
75+ < p>
76+ < label>
77+ Phone
78+ < input
79+ type= " tel"
80+ name= " phone_number"
81+ onBlur= {this .form .handleBlurEvent }
82+ onChange= {this .form .handleFieldsChange }
83+ value= {this .state .fields .phone_number }
84+ / >
85+ < / label>
86+ < label className= " error" >
87+ {this .state .inputErrors .phone_number ? this .state .inputErrors .phone_number .message : " " }
88+ < / label>
89+ < / p>
90+ < p>
91+ < button type= " submit" > Submit< / button>
92+ < / p>
93+ < / form>
94+ < / React .Fragment > )
95+ }
96+ }
97+ ```
98+
99+ > ** Note:**
100+ > This package is fully compatible with libraries like [ Material UI] ( https://material-ui.com/ ) , etc.
12101
13102## Custom attribute name
14103
@@ -17,15 +106,15 @@ Refer the below example to override the attribute name
17106``` html
18107 <input
19108 type =" text"
20- name =" customer_name "
109+ name =" name "
21110 onBlur ={this.form.handleBlurEvent}
22111 onChange ={this.form.handleFieldsChange}
23- value ={this.state.fields.customer_name }
24- data-attribute-name =" CUSTOMER NAME"
112+ value ={this.state.fields.name }
113+ data-attribute-name =" USER NAME"
25114 />
26115```
27116
28- The output will be like, "The CUSTOMER NAME field is required."
117+ The output will be like, "The USER NAME field is required."
29118
30119## License
31120
0 commit comments