Form validation for the last name | React

The name of the pictureThe name of the pictureThe name of the pictureClash 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')
)






share|improve this question















  • 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

















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')
)






share|improve this question















  • 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













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')
)






share|improve this question











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')
)








share|improve this question










share|improve this question




share|improve this question









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













  • 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
















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

Greedy Best First Search implementation in Rust

Function to Return a JSON Like Objects Using VBA Collections and Arrays

C++11 CLH Lock Implementation