检查字符串的首字母是否大写

时间:2019-06-04 13:19:56

标签: reactjs

我正在尝试检查字符串的首字母是否大写。

charAt()对我不起作用,如果您还有其他解决方案来检查第一个字母是否为大写字母,我将不胜感激。

我已经尝试过检查-this.state.firstName[0] === this.state.firstName[0].toUpperCase(),但它也对我不起作用


class Profile extends Component {
  state = {
    details: {
      firstName: '',
      lastName: '',
      ID: '',
      Email: ''
    }
  };

  OnSubmit = e => {
    e.preventDefault();
    if (
      this.state.firstName.charAt(0) ===
      this.state.firstName.charAt(0).toUpperCase()
    ) {
      console.log('error');
    }
  };

  OnChange = e => {
    e.preventDefault();
    this.setState({
      details: { ...this.state.details, [e.target.name]: e.target.value }
    });
  };

  render() {
    return (
      <div>
        <div className="container text-center mt-4" style={{ width: '500px' }}>
          <form className="px-4 py-3" onSubmit={this.OnSubmit}>
            <div className="form-group">
              <label>First Name:</label>
              <input
                type="text"
                className="form-control"
                onChange={this.OnChange}
                name="firstName"
              />
            </div>
            <div className="form-group">
              <label>Last Name:</label>
              <input
                type="text"
                className="form-control"
                onChange={this.OnChange}
                name="lastName"
              />
            </div>
            <div className="form-group">
              <label>ID Number:</label>
              <input
                type="text"
                className="form-control"
                onChange={this.OnChange}
                name="ID"
              />
            </div>
            <div className="form-group">
              <label>Email:</label>
              <input
                type="text"
                className="form-control"
                onChange={this.OnChange}
                name="Email"
              />
            </div>
            <button type="submit" className="btn btn-secondary mt-3">
              Check
            </button>
          </form>
        </div>
      </div>
    );
  }
}

export default Profile;

charAt()给我一个错误-“未被捕获的TypeError:无法读取未定义的属性'charAt'”

2 个答案:

答案 0 :(得分:2)

一种简单的方法来检查字符串中的第一个字母是否为大写。查看下面的演示

import React, { Component } from 'react';
import { render } from 'react-dom';
import Hello from './Hello';
import './style.css';

class App extends Component {
  constructor() {
    super();
    this.state = {
      name: 'React'
    };
  }

  componentWillMount()
  {
    if (this.state.name[0] >= 'A' && this.state.name[0] <= 'Z')
      this.setState({ name: "First letter is uppercase" })
    else
      this.setState({ name: "First letter is NOT uppercase" })
  }

  render() {
    return (
      <div>
        <p>
          {this.state.name}
        </p>
      </div>
    );
  }
}

render(<App />, document.getElementById('root'));

Demo

答案 1 :(得分:2)

在状态下,您有详细信息对象,可以通过详细信息对象访问名字和姓氏。

class Profile extends Component {
      state = {
        details: {
          firstName: '',
          lastName: '',
          ID: '',
          Email: ''
        }
      };

      OnSubmit = e => {
        e.preventDefault();
        if (
          this.state.details.firstName.charAt(0) ===
          this.state.details.firstName.charAt(0).toUpperCase()
        ) {
          console.log('error');
        }
      };

      OnChange = e => {
        e.preventDefault();
        this.setState({
          details: { ...this.state.details, [e.target.name]: e.target.value }
        });
      };

      render() {
        return (
          <div>
            <div className="container text-center mt-4" style={{ width: '500px' }}>
              <form className="px-4 py-3" onSubmit={this.OnSubmit}>
                <div className="form-group">
                  <label>First Name:</label>
                  <input
                    type="text"
                    className="form-control"
                    onChange={this.OnChange}
                    name="firstName"
                  />
                </div>
                <div className="form-group">
                  <label>Last Name:</label>
                  <input
                    type="text"
                    className="form-control"
                    onChange={this.OnChange}
                    name="lastName"
                  />
                </div>
                <div className="form-group">
                  <label>ID Number:</label>
                  <input
                    type="text"
                    className="form-control"
                    onChange={this.OnChange}
                    name="ID"
                  />
                </div>
                <div className="form-group">
                  <label>Email:</label>
                  <input
                    type="text"
                    className="form-control"
                    onChange={this.OnChange}
                    name="Email"
                  />
                </div>
                <button type="submit" className="btn btn-secondary mt-3">
                  Check
                </button>
              </form>
            </div>
          </div>
        );
      }
    }

    export default Profile;