Repository navigation
Expand file tree
/
Copy pathLesson15_Validate.html
More file actions
138 lines (126 loc) · 3.42 KB
/
Copy pathLesson15_Validate.html
File metadata and controls
138 lines (126 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<head>
<script src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script>
<link href="https://fonts.googleapis.com/css?family=Roboto+Slab" rel="stylesheet">
<link rel="icon" href="images/checked.png" type="image/gif" sizes="16x16">
</head>
<style>
body{
height: 100vh;
}
#root{
display: flex;
flex-direction: column;
justify-content: space-around;
align-items: center;
color: black;
background: url('images/desktop.jpg') no-repeat;
background-size: 100% 100vh;
font-family: 'Roboto Slab', serif;
}
.title{
font-size: calc(35px + 1vw);
}
.mainDiv{
display: flex;
flex-direction: column;
justify-content: space-around;
align-items: center;
height: 50vh;
}
form{
max-width: 70%;
}
input{
max-width: 70%;
align-self: center;
}
button{
align-self: center;
text-align: center;
border-radius: 10px;
color: black;
padding: 10px;
min-width: 100px;
max-width: 70%;
font-family: 'Roboto Slab', serif;
font-size: 30px */
}
label{
font-size: 20px;
}
h1{
font-size: 30px;
}
form{
text-align: center;
display: flex;
flex-direction: column;
justify-content: space-around;
align-content: center;
min-height: 40vh;
}
</style>
<body id="root"></body>
<script type="text/babel">
class NameForm extends React.Component{
state = {error: null};
handleSubmit = (e) => {
e.preventDefault();
const value = event.target.elements.username.value;
const error = this.props.getErrorMessage(value);
if(error){
alert(`error: ${error}`);
}else{
alert(`success: ${value}`);
}
}
handleChange = e => {
const {value} = e.target;
this.setState({
error: this.props.getErrorMessage(value)
})
}
componentDidMount(){
this.setState({
error: this.props.getErrorMessage(''),
})
}
render (){
const {error} = this.state;
return (
<form onSubmit={this.handleSubmit}>
<h1>React Front-End Validation</h1>
<label>
Name:
</label>
{error ? <div style={{color: 'red'}}>{error}</div> : null}
<input
type="text"
onChange={this.handleChange}
name="username"
ref={node => (this.inputNode = node)}
/>
<button
disabled={Boolean(error)}
type="submit">Submit</button>
</form>
)
}
}
ReactDOM.render(
<NameForm
getErrorMessage={value => {
if(value.length < 3){
return "Name must be at least 3 characters long";
}
if (!value.includes('p')){
return 'Name must include "p"';
}
return null;
}}
/>,
document.getElementById('root')
)
</script>