Super simple email validation with javascript

前端 未结 8 729
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-14 00:32

I\'m making a really simple email validation script that basically just checks the following

  1. that the email isn\'t blank
  2. the the email contains an @ s
8条回答
  •  旧巷少年郎
    2020-12-14 01:08

    What others have suggested should work fine, but if you want to keep things simple, try this:

    var booking_email = $('input[name=booking_email]').val();
    
    if( /(.+)@(.+){2,}\.(.+){2,}/.test(booking_email) ){
      // valid email
    } else {
      // invalid email
    }
    

    Even if you decide to go with something more robust, it should help you understand how simple regex can be at times. :)

提交回复
热议问题