console.log在它之后停止警报

时间:2019-07-08 03:02:29

标签: javascript version-control

我是Java语言的新手,我正尝试从中创建联系人并对其进行验证,但是,正如您在下面看到的那样,我不知道它是否是...

console.log('Email is valid!');

...或其他任何内容,但之后警报的其他部分将无法工作!谁知道怎么修它?感谢您的帮助!

if (name === '') {
  alert('Pls enter your name!')
} else if (name.length < 2) {
  alert('Pls enter a valid name! Longer than 2 letters');
} else if (name.length > 20) {
  alert('Pls enter a valid name! Less than 20 letters');
} else if (email === '') {
  alert('Empty Email. Pls enter your email!')
} else if (!validateEmail(email)) {
  alert('Email is not valid!');
} else if (validateEmail(email)) {
  console.log('Email is valid!');
} else if (message.length < 1) {
  alert('Pls enter a Message!')
} else if (message.length < 5) {
  alert('Pls enter a message longer than 5 letters!')

1 个答案:

答案 0 :(得分:0)

请阅读if-else-if的工作方式,它只会执行所有条件的 one 并立即退出。如果要执行所有警报,只需执行一堆ifs

if (name === '') {
  alert('Pls enter your name!')
} 

if (name.length < 2) {
  alert('Pls enter a valid name! Longer than 2 letters');
} 

if (name.length > 20) {
  alert('Pls enter a valid name! Less than 20 letters');
} 

if (email === '') {
  alert('Empty Email. Pls enter your email!')
} 

if (!validateEmail(email)) {
  alert('Email is not valid!');
} 

if (validateEmail(email)) {
  console.log('Email is valid!');
} 

if (message.length < 1) {
  alert('Pls enter a Message!')
} 

if (message.length < 5) {
  alert('Pls enter a message longer than 5 letters!')
}