css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation


Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

              body {
  margin: 1em;
              class App extends React.Component {
  constructor() {
    this.state = {
      email: '',
      username: '',
      password: '',
      formErrors: {email: '', username:'', password: ''},
      formValidity: {email: false, username: false, password: false},
      canSubmit: false,
    this.handleChange = this.handleChange.bind(this)
  // triggered everytime value changes in our textboxes
  handleChange(event) {
    const {name, value} = event.target
      // use dynamic name value to set our state object property
      [name]: value
    }, function(){ this.validateField(name, value)})

  validateField(name, value) {
    const fieldValidationErrors = this.state.formErrors
    const validity = this.state.formValidity
    const isEmail = name === "email"
    const isPassword = name === "password"
    const emailTest = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/i

    validity[name] = value.length >0
    fieldValidationErrors[name] = validity[name] ? '': `${name} is required and cannot be empty`;

    if(validity[name]) {
        validity[name] = value.length >= 5;
        fieldValidationErrors[name] = validity[name] ? '': `${name} should be 5 characters or more`;
        validity[name] = emailTest.test(value);
        fieldValidationErrors[name] = validity[name] ? '' : `${name} should be a valid email address`;
      formErrors: fieldValidationErrors,
      formValidity: validity,
    }, () => this.canSubmit())

  canSubmit() {
    this.setState({canSubmit: this.state.formValidity.email && this.state.formValidity.username && this.state.formValidity.password})

  errorClass(error) {
    return(error.length === 0 ? '' : 'is-invalid');
  // triggered on submit
  handleSubmit = (event) => {
    // get our const values by destructuring state
    // see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Object_destructuring
    const { email, username, password } = this.state
    // regular javascript alert function
    alert(`Your registration detail: \n 
           Email: ${email} \n 
           Username: ${username} \n
           Password: ${password}`)
  render() {    
    return (
      <h1>Registration Form</h1>
      <p>Please fill in all textboxes</p> 
      <form onSubmit={this.handleSubmit}>
        <div className="form-group">
          <label htmlFor="email">Email address</label>
            className={`form-control ${this.errorClass(this.state.formErrors.email)}`}
            placeholder="Enter email"
          <div className="invalid-feedback">{this.state.formErrors.email}</div>
        <div className="form-group">
          <label htmlFor="username">Username</label>
            className={`form-control ${this.errorClass(this.state.formErrors.username)}`}
            placeholder="Enter username"
          <div className="invalid-feedback">{this.state.formErrors.username}</div>
        <div className="form-group">
          <label htmlFor="password">Password</label>
            className={`form-control ${this.errorClass(this.state.formErrors.password)}`}
            name ="password"
            placeholder="Enter password"
          <div className="invalid-feedback">{this.state.formErrors.password}</div>
        <button className="btn btn-success btn-block" disabled={!this.state.canSubmit}>Sign up</button>

ReactDOM.render(<App />, document.getElementById('root'))
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................