简体   繁体   English

function 检查验证是否为真,然后执行此操作

[英]function that checks if validation is true, then do this

I'm trying to make this work.我正在努力完成这项工作。

JS validation: JS 验证:

function validation() {
  var fname = document.getElementById('fname').value;
  if (fname == '') {
    document.getElementById('fn').innerHTML = 'Please enter first name.';
  }
  var lname = document.getElementById('lname').value;
  if (lname == '') {
    document.getElementById('ln').innerHTML = 'Please enter last name.';
  }
  var birth = document.getElementById('birthdate').value;
  if (birth == '') {
    document.getElementById('bday').innerHTML = 'Please enter birthdate.';
  }
  var gender = document.getElementById('gender').value;
  if (gender == 'select') {
    document.getElementById('gndr').innerHTML = 'Please choose your gender.';
  }
  var username = document.getElementById('username').value;
  if (username == '') {
    document.getElementById('usr').innerHTML = 'Please enter username.';
  }
  var email = document.getElementById('email').value;
  if (email == '') {
  }
  var econf = document.getElementById('econf').value;
  if (econf == '') {
    document.getElementById('rt-eml').innerHTML = 'Please confirm your email.';
  }
  if (econf != email) {
    document.getElementById('rt-eml').innerHTML = 'Email did not match.';
  }
  var password = document.getElementById('password').value;
  if (password == '') {
    document.getElementById('pass').innerHTML = 'Please enter password.';
  }
  var pconf = document.getElementById('pconf').value;
  if (pconf == '') {
    document.getElementById('rt-pass').innerHTML =
      'Please confirm your password.';
  }
  if (pconf != password) {
    document.getElementById('rt-pass').innerHTML =
      'Password did not match. Try again. ';
  } 
}

I want my check function to check if validation is true then change the innerHTML of success to You have successfully created an account.我希望我的check function 检查validation是否为真,然后将successinnerHTML更改为You have successfully created an account.

JS check function: JS check function:

function check() {
  if (validation === true) {
    document.getElementById('success').innerHTML =
      'Your account has been successfully created.';
  }
}

I know it's kinda messed up and I admit I'm still new to this.我知道这有点搞砸了,我承认我还是新手。

A common way to do it is to initialize a boolean to true , if any field is empty or not valid then you assign false to this boolean.一种常见的方法是将 boolean 初始化为true ,如果任何字段为空或无效,则将false分配给此 boolean。 You return its value at the end of the validation function.在验证 function 结束时返回它的值。

function check() {
  //no need to check === true here
  if (validation()) {
    document.getElementById('success').innerHTML =
      'Your account has been successfully created.';
  }
}

function validation() {
  let isValid = true;
  var fname = document.getElementById('fname').value;
  if (fname == '') {
    document.getElementById('fn').innerHTML = 'Please enter first name.';
    isValid = false;
  }
  var lname = document.getElementById('lname').value;
  if (lname == '') {
    document.getElementById('ln').innerHTML = 'Please enter last name.';
    isValid = false;
  }
  var birth = document.getElementById('birthdate').value;
  if (birth == '') {
    document.getElementById('bday').innerHTML = 'Please enter birthdate.';
    isValid = false;
  }
  var gender = document.getElementById('gender').value;
  if (gender == 'select') {
    document.getElementById('gndr').innerHTML = 'Please choose your gender.';
    isValid = false;
  }
  var username = document.getElementById('username').value;
  if (username == '') {
    document.getElementById('usr').innerHTML = 'Please enter username.';
    isValid = false;
  }
  var email = document.getElementById('email').value;
  if (email == '') {
    isValid = false;
  }
  var econf = document.getElementById('econf').value;
  if (econf == '') {
    document.getElementById('rt-eml').innerHTML = 'Please confirm your email.';
    isValid = false;
  }
  if (econf != email) {
    document.getElementById('rt-eml').innerHTML = 'Email did not match.';
    isValid = false;
  }
  var password = document.getElementById('password').value;
  if (password == '') {
    document.getElementById('pass').innerHTML = 'Please enter password.';
    isValid = false;
  }
  var pconf = document.getElementById('pconf').value;
  if (pconf == '') {
    document.getElementById('rt-pass').innerHTML =
      'Please confirm your password.';
    isValid = false;
  }
  if (pconf != password) {
    document.getElementById('rt-pass').innerHTML =
      'Password did not match. Try again. ';
    isValid = false;
  }
  return isValid;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何对数据库变量进行完整性检查和验证检查? - how to do sanity and validation checks on database variables? 如果验证 function 为真,则调用方法 - calling a method if the validation function is true 验证函数后返回“ true” - Returning 'true' after validation function 检查输入值是否为回文的 Function 始终返回 true - Function that checks whether input value is palindrome or not always returns true 如何在不循环两次的情况下对数组执行两次验证检查 - How to do two validation checks on an array without looping two times 如果验证函数返回true,则关注下一个元素 - Focus next element if validation function returns true 我可以编写一个 function 来检查用户点击的答案是真还是假,并在完成后加载新问题? - Can I write a function that checks if an answer pressed by a user clicked is true or false and load the new question once it is done? 通过 7 中的 4,尝试创建一个 function 来检查“instock”与“weekly average”的数组,返回一个显示“true/false”的值 - Passing 4 out of 7, trying to create a function that checks an array of "instock" vs "weekly average" returns a value that shows "true/false" 在哪里进行表单提交检查以进行重定向:onunload或in ajax函数? - Where to do checks on form submit for redirection: onunload or in ajax function? 如何在 function 中包装 Ajax 权限检查? - How do I wrap Ajax rights checks in a function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM