1import React from 'react';
2import {sendFormData} from '../services/';
3
4class Signup extends React.Component{
5 constructor(props){
6 super(props);
7 this.state = {
8 isDisabled:true
9 }
10 this.submitForm = this.submitForm.bind(this);
11 }
12 validateEmail(email){
13 const pattern = /[a-zA-Z0-9]+[\.]?([a-zA-Z0-9]+)?[\@][a-z]{3,9}[\.][a-z]{2,5}/g;
14 const result = pattern.test(email);
15 if(result===true){
16 this.setState({
17 emailError:false,
18 email:email
19 })
20 } else{
21 this.setState({
22 emailError:true
23 })
24 }
25 }
26 handleChange(e){
27 const target = e.target;
28 const value = target.type === 'checkbox' ? target.checked : target.value;
29 const name = target.name;
30 this.setState({
31 [name]: value
32 });
33 if(e.target.name==='firstname'){
34 if(e.target.value==='' || e.target.value===null ){
35 this.setState({
36 firstnameError:true
37 })
38 } else {
39 this.setState({
40 firstnameError:false,
41 firstName:e.target.value
42 })
43 }
44 }
45 if(e.target.name==='lastname'){
46 if(e.target.value==='' || e.target.value===null){
47 this.setState({
48 lastnameError:true
49 })
50 } else {
51 this.setState({
52 lastnameError:false,
53 lastName:e.target.value
54 })
55 }
56 }
57 if(e.target.name==='email'){
58 this.validateEmail(e.target.value);
59 }
60 if(e.target.name==='password'){
61 if(e.target.value==='' || e.target.value===null){
62 this.setState({
63 passwordError:true
64 })
65 } else {
66 this.setState({
67 passwordError:false,
68 password:e.target.value
69 })
70 }
71 }
72 if(this.state.firstnameError===false && this.state.lastnameError===false &&
73 this.state.emailError===false && this.state.passwordError===false){
74 this.setState({
75 isDisabled:false
76 })
77 }
78}
79submitForm(e){
80 e.preventDefault();
81 const data = {
82 firstName: this.state.firstName,
83 lastName: this.state.lastName,
84 email: this.state.email,
85 password: this.state.password
86 }
87 sendFormData(data).then(res=>{
88 if(res.status===200){
89 alert(res.data);
90 this.props.history.push('/');
91 }else{
92
93 }
94 });
95 }
96render(){
97return(
98 <div className="container">
99 <div className="card card-login mx-auto mt-5">
100 <div className="card-header">Register here</div>
101 <div className="card-body">
102 <form id="signup-form">
103 <div className="form-group">
104 <div className="form-label-group">
105 <input type="text" id="firstname" name="firstname" className="form-control" placeholder="Enter firstname" onChange={(e)=>{this.handleChange(e)}} />
106 <label htmlFor="firstname">firstname</label>
107 {this.state.firstnameError ? <span style={{color: "red"}}>Please Enter some value</span> : ''}
108 </div>
109 </div>
110 <div className="form-group">
111 <div className="form-label-group">
112 <input type="text" id="lastname" name="lastname" className="form-control" placeholder="Enter lastname" onChange={(e)=>{this.handleChange(e)}} />
113 <label htmlFor="lastname">lastname</label>
114 {this.state.lastnameError ? <span style={{color: "red"}}>Please Enter some value</span> : ''}
115 </div>
116 </div>
117 <div className="form-group">
118 <div className="form-label-group">
119 <input type="email" id="email" name="email" className="form-control" placeholder="Enter your email" onChange={(e)=>{this.handleChange(e)}} />
120 <label htmlFor="email">email</label>
121 {this.state.emailError ? <span style={{color: "red"}}>Please Enter valid email address</span> : ''}
122 </div>
123 </div>
124 <div className="form-group">
125 <div className="form-label-group">
126 <input type="password" id="password" name="password" className="form-control" placeholder="Password" onChange={(e)=>{this.handleChange(e)}} />
127 <label htmlFor="password">Password</label>
128 {this.state.passwordError ? <span style={{color: "red"}}>Please enter some value</span> : ''}
129 </div>
130 </div>
131 <button className="btn btn-primary btn-block" disabled={this.state.isDisabled} onClick={this.submitForm}>Signup</button>
132 </form>
133 </div>
134 </div>
135 </div>
136 );
137 }
138}
139export default Signup;
140
1 class Test extends React.Component {
2 constructor(props){
3 super(props);
4
5 this.state = {
6 fields: {},
7 errors: {}
8 }
9 }
10
11 handleValidation(){
12 let fields = this.state.fields;
13 let errors = {};
14 let formIsValid = true;
15
16 //Name
17 if(!fields["name"]){
18 formIsValid = false;
19 errors["name"] = "Cannot be empty";
20 }
21
22 if(typeof fields["name"] !== "undefined"){
23 if(!fields["name"].match(/^[a-zA-Z]+$/)){
24 formIsValid = false;
25 errors["name"] = "Only letters";
26 }
27 }
28
29 //Email
30 if(!fields["email"]){
31 formIsValid = false;
32 errors["email"] = "Cannot be empty";
33 }
34
35 if(typeof fields["email"] !== "undefined"){
36 let lastAtPos = fields["email"].lastIndexOf('@');
37 let lastDotPos = fields["email"].lastIndexOf('.');
38
39 if (!(lastAtPos < lastDotPos && lastAtPos > 0 && fields["email"].indexOf('@@') == -1 && lastDotPos > 2 && (fields["email"].length - lastDotPos) > 2)) {
40 formIsValid = false;
41 errors["email"] = "Email is not valid";
42 }
43 }
44
45 this.setState({errors: errors});
46 return formIsValid;
47 }
48
49 contactSubmit(e){
50 e.preventDefault();
51
52 if(this.handleValidation()){
53 alert("Form submitted");
54 }else{
55 alert("Form has errors.")
56 }
57
58 }
59
60 handleChange(field, e){
61 let fields = this.state.fields;
62 fields[field] = e.target.value;
63 this.setState({fields});
64 }
65
66 render(){
67 return (
68 <div>
69 <form name="contactform" className="contactform" onSubmit= {this.contactSubmit.bind(this)}>
70 <div className="col-md-6">
71 <fieldset>
72 <input ref="name" type="text" size="30" placeholder="Name" onChange={this.handleChange.bind(this, "name")} value={this.state.fields["name"]}/>
73 <span style={{color: "red"}}>{this.state.errors["name"]}</span>
74 <br/>
75 <input refs="email" type="text" size="30" placeholder="Email" onChange={this.handleChange.bind(this, "email")} value={this.state.fields["email"]}/>
76 <span style={{color: "red"}}>{this.state.errors["email"]}</span>
77 <br/>
78 <input refs="phone" type="text" size="30" placeholder="Phone" onChange={this.handleChange.bind(this, "phone")} value={this.state.fields["phone"]}/>
79 <br/>
80 <input refs="address" type="text" size="30" placeholder="Address" onChange={this.handleChange.bind(this, "address")} value={this.state.fields["address"]}/>
81 <br/>
82 </fieldset>
83 </div>
84
85 </form>
86 </div>
87 )
88 }
89 }
90
91 React.render(<Test />, document.getElementById('container'));
92
93