Form validation for multiple inputs - 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
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')
)






share|improve this question



























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






    share|improve this question























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






      share|improve this question













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








      share|improve this question












      share|improve this question




      share|improve this question








      edited May 23 at 21:38









      200_success

      123k14143399




      123k14143399









      asked May 23 at 13:15









      Kate Herasimenak

      3016




      3016

























          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%2f195021%2fform-validation-for-multiple-inputs-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%2f195021%2fform-validation-for-multiple-inputs-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