Form validation for the last name | React
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
I created an example of a simple form for a last name input in React, where I can check the validation of the data at the time of input.
The last name should contain only small and capital letters, numbers, special characters, etc. If the user enters other characters, the error text is displayed.
I will be grateful for tips on improving the style.
index.js:
import React from 'react'
import render from 'react-dom'
const ErrorOutput = props =>
if (!(props.case).match(/^[a-zA-Z]+$/) && (props.case).length > 0)
return <span>Letters only.</span>
return <span></span>
class App extends React.Component
constructor(props)
super(props)
this.state = value: ''
handleValidation(e)
this.setState(
value: e.target.value
)
render()
const inputValue = this.state.value
return (
<form>
<label>
Last name:
</label>
<input
type='text'
name ='lastName'
value = this.state.value
onChange = this.handleValidation.bind(this)
/>
<ErrorOutput case=inputValue />
</form>
)
render(
<App />,
document.getElementById('root')
)
javascript beginner validation react.js
add a comment |Â
up vote
2
down vote
favorite
I created an example of a simple form for a last name input in React, where I can check the validation of the data at the time of input.
The last name should contain only small and capital letters, numbers, special characters, etc. If the user enters other characters, the error text is displayed.
I will be grateful for tips on improving the style.
index.js:
import React from 'react'
import render from 'react-dom'
const ErrorOutput = props =>
if (!(props.case).match(/^[a-zA-Z]+$/) && (props.case).length > 0)
return <span>Letters only.</span>
return <span></span>
class App extends React.Component
constructor(props)
super(props)
this.state = value: ''
handleValidation(e)
this.setState(
value: e.target.value
)
render()
const inputValue = this.state.value
return (
<form>
<label>
Last name:
</label>
<input
type='text'
name ='lastName'
value = this.state.value
onChange = this.handleValidation.bind(this)
/>
<ErrorOutput case=inputValue />
</form>
)
render(
<App />,
document.getElementById('root')
)
javascript beginner validation react.js
1
This code looks good! I think the main thing to consider is how you'd reuse it once you start adding extra fields - you might want to take the whole label/input/error output combination and encapsulate it as a re-usable component. As a standalone piece of code though, I can't find any obvious improvements to make :)
â Joe Clay
May 23 at 11:46
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I created an example of a simple form for a last name input in React, where I can check the validation of the data at the time of input.
The last name should contain only small and capital letters, numbers, special characters, etc. If the user enters other characters, the error text is displayed.
I will be grateful for tips on improving the style.
index.js:
import React from 'react'
import render from 'react-dom'
const ErrorOutput = props =>
if (!(props.case).match(/^[a-zA-Z]+$/) && (props.case).length > 0)
return <span>Letters only.</span>
return <span></span>
class App extends React.Component
constructor(props)
super(props)
this.state = value: ''
handleValidation(e)
this.setState(
value: e.target.value
)
render()
const inputValue = this.state.value
return (
<form>
<label>
Last name:
</label>
<input
type='text'
name ='lastName'
value = this.state.value
onChange = this.handleValidation.bind(this)
/>
<ErrorOutput case=inputValue />
</form>
)
render(
<App />,
document.getElementById('root')
)
javascript beginner validation react.js
I created an example of a simple form for a last name input in React, where I can check the validation of the data at the time of input.
The last name should contain only small and capital letters, numbers, special characters, etc. If the user enters other characters, the error text is displayed.
I will be grateful for tips on improving the style.
index.js:
import React from 'react'
import render from 'react-dom'
const ErrorOutput = props =>
if (!(props.case).match(/^[a-zA-Z]+$/) && (props.case).length > 0)
return <span>Letters only.</span>
return <span></span>
class App extends React.Component
constructor(props)
super(props)
this.state = value: ''
handleValidation(e)
this.setState(
value: e.target.value
)
render()
const inputValue = this.state.value
return (
<form>
<label>
Last name:
</label>
<input
type='text'
name ='lastName'
value = this.state.value
onChange = this.handleValidation.bind(this)
/>
<ErrorOutput case=inputValue />
</form>
)
render(
<App />,
document.getElementById('root')
)
javascript beginner validation react.js
asked May 23 at 9:58
Kate Herasimenak
3016
3016
1
This code looks good! I think the main thing to consider is how you'd reuse it once you start adding extra fields - you might want to take the whole label/input/error output combination and encapsulate it as a re-usable component. As a standalone piece of code though, I can't find any obvious improvements to make :)
â Joe Clay
May 23 at 11:46
add a comment |Â
1
This code looks good! I think the main thing to consider is how you'd reuse it once you start adding extra fields - you might want to take the whole label/input/error output combination and encapsulate it as a re-usable component. As a standalone piece of code though, I can't find any obvious improvements to make :)
â Joe Clay
May 23 at 11:46
1
1
This code looks good! I think the main thing to consider is how you'd reuse it once you start adding extra fields - you might want to take the whole label/input/error output combination and encapsulate it as a re-usable component. As a standalone piece of code though, I can't find any obvious improvements to make :)
â Joe Clay
May 23 at 11:46
This code looks good! I think the main thing to consider is how you'd reuse it once you start adding extra fields - you might want to take the whole label/input/error output combination and encapsulate it as a re-usable component. As a standalone piece of code though, I can't find any obvious improvements to make :)
â Joe Clay
May 23 at 11:46
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195010%2fform-validation-for-the-last-name-react%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
This code looks good! I think the main thing to consider is how you'd reuse it once you start adding extra fields - you might want to take the whole label/input/error output combination and encapsulate it as a re-usable component. As a standalone piece of code though, I can't find any obvious improvements to make :)
â Joe Clay
May 23 at 11:46