Form validation for multiple inputs - react
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
I created an example of a simple form for multiple inputs (name and phone number) in React. If user enters invalid data in input field, error text is displayed near the same field.
I learned a lot of different examples of the form validation using React and I don't understand where is the better place for validation checking in form.
import React from 'react'
import render from 'react-dom'
const ErrorOutput = props =>
let name = props.name
let inputValue = props.case
if (name === 'firstName')
if (!inputValue.match(/^[a-zA-Z]+$/) && inputValue.length > 0)
return <span>Letters only</span>
return <span></span>
if (name === 'telNo')
if(!inputValue.match(/^[0-9]+$/) && inputValue.length > 0)
return <span>Numbers only</span>
return <span></span>
class App extends React.Component
constructor(props)
super(props)
this.state =
firstName: '',
telNo: ''
handleValidation(e)
this.setState(
[e.target.name]: e.target.value
)
render()
return (
<form>
<label>
First name:
</label>
<input
type='text'
name ='firstName'
value = this.state.firstName
onChange = this.handleValidation.bind(this)
/>
<ErrorOutput case=this.state.firstName name='firstName' />
<label>
Phone number:
</label>
<input
type='tel'
name ='telNo'
value = this.state.telNo
onChange = this.handleValidation.bind(this)
/>
<ErrorOutput case=this.state.telNo name='telNo' />
</form>
)
render(
<App />,
document.getElementById('root')
)
beginner validation form react.js jsx
add a comment |Â
up vote
1
down vote
favorite
I created an example of a simple form for multiple inputs (name and phone number) in React. If user enters invalid data in input field, error text is displayed near the same field.
I learned a lot of different examples of the form validation using React and I don't understand where is the better place for validation checking in form.
import React from 'react'
import render from 'react-dom'
const ErrorOutput = props =>
let name = props.name
let inputValue = props.case
if (name === 'firstName')
if (!inputValue.match(/^[a-zA-Z]+$/) && inputValue.length > 0)
return <span>Letters only</span>
return <span></span>
if (name === 'telNo')
if(!inputValue.match(/^[0-9]+$/) && inputValue.length > 0)
return <span>Numbers only</span>
return <span></span>
class App extends React.Component
constructor(props)
super(props)
this.state =
firstName: '',
telNo: ''
handleValidation(e)
this.setState(
[e.target.name]: e.target.value
)
render()
return (
<form>
<label>
First name:
</label>
<input
type='text'
name ='firstName'
value = this.state.firstName
onChange = this.handleValidation.bind(this)
/>
<ErrorOutput case=this.state.firstName name='firstName' />
<label>
Phone number:
</label>
<input
type='tel'
name ='telNo'
value = this.state.telNo
onChange = this.handleValidation.bind(this)
/>
<ErrorOutput case=this.state.telNo name='telNo' />
</form>
)
render(
<App />,
document.getElementById('root')
)
beginner validation form react.js jsx
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I created an example of a simple form for multiple inputs (name and phone number) in React. If user enters invalid data in input field, error text is displayed near the same field.
I learned a lot of different examples of the form validation using React and I don't understand where is the better place for validation checking in form.
import React from 'react'
import render from 'react-dom'
const ErrorOutput = props =>
let name = props.name
let inputValue = props.case
if (name === 'firstName')
if (!inputValue.match(/^[a-zA-Z]+$/) && inputValue.length > 0)
return <span>Letters only</span>
return <span></span>
if (name === 'telNo')
if(!inputValue.match(/^[0-9]+$/) && inputValue.length > 0)
return <span>Numbers only</span>
return <span></span>
class App extends React.Component
constructor(props)
super(props)
this.state =
firstName: '',
telNo: ''
handleValidation(e)
this.setState(
[e.target.name]: e.target.value
)
render()
return (
<form>
<label>
First name:
</label>
<input
type='text'
name ='firstName'
value = this.state.firstName
onChange = this.handleValidation.bind(this)
/>
<ErrorOutput case=this.state.firstName name='firstName' />
<label>
Phone number:
</label>
<input
type='tel'
name ='telNo'
value = this.state.telNo
onChange = this.handleValidation.bind(this)
/>
<ErrorOutput case=this.state.telNo name='telNo' />
</form>
)
render(
<App />,
document.getElementById('root')
)
beginner validation form react.js jsx
I created an example of a simple form for multiple inputs (name and phone number) in React. If user enters invalid data in input field, error text is displayed near the same field.
I learned a lot of different examples of the form validation using React and I don't understand where is the better place for validation checking in form.
import React from 'react'
import render from 'react-dom'
const ErrorOutput = props =>
let name = props.name
let inputValue = props.case
if (name === 'firstName')
if (!inputValue.match(/^[a-zA-Z]+$/) && inputValue.length > 0)
return <span>Letters only</span>
return <span></span>
if (name === 'telNo')
if(!inputValue.match(/^[0-9]+$/) && inputValue.length > 0)
return <span>Numbers only</span>
return <span></span>
class App extends React.Component
constructor(props)
super(props)
this.state =
firstName: '',
telNo: ''
handleValidation(e)
this.setState(
[e.target.name]: e.target.value
)
render()
return (
<form>
<label>
First name:
</label>
<input
type='text'
name ='firstName'
value = this.state.firstName
onChange = this.handleValidation.bind(this)
/>
<ErrorOutput case=this.state.firstName name='firstName' />
<label>
Phone number:
</label>
<input
type='tel'
name ='telNo'
value = this.state.telNo
onChange = this.handleValidation.bind(this)
/>
<ErrorOutput case=this.state.telNo name='telNo' />
</form>
)
render(
<App />,
document.getElementById('root')
)
beginner validation form react.js jsx
edited May 23 at 21:38
200_success
123k14143399
123k14143399
asked May 23 at 13:15
Kate Herasimenak
3016
3016
add a comment |Â
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%2f195021%2fform-validation-for-multiple-inputs-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