首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JS如何设置变量长度限制

问JS如何设置变量长度限制
EN

Stack Overflow用户
提问于 2021-11-06 17:11:58
回答 1查看 54关注 0票数 0

我被卡住了:/.我想如果例如#名字长度大于20来说明我做的事情,如果它是空的,但我不知道如何,如果在座的任何人知道,我会很高兴!

代码语言:javascript
复制
     $("#register").click(function(e) {
          e.preventDefault();
          var sex = 'male';
          var firstname = '';
          var lastname = '';
          var date = '';
          if ($('.container[data-sex="female"] input:checked').val() == 'on') {
               sex = 'female';
          }
          if ($('#firstname').val() != '' && $('#firstname').val() != null) {
              firstname = $('#firstname').val(); 
          }else {
               return Information('Firstname can\'t be empty!');
          }
          if ($('#lastname').val() != '' && $('#lastname').val() != null) {
               lastname = $('#lastname').val(); 
          }else {
                return Information('Lastname can\'t be empty!');
          }
          if ($('#date').val() != '' && $('#date').val() != null) {
               date = $('#date').val(); 
          }else {
                return Information('Date can\'t be empty!');
          }
          $('#core').fadeOut(300);
          $.post('https://lion-register/CreateCharacter', JSON.stringify({
               firstname : firstname,
               lastname : lastname,
               sex : sex,
               date : date,
               queue : MX.CurrentCharacter
          }));
     });
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-06 18:14:30

使用纯JavaScript,如下所示:

代码语言:javascript
复制
var value = document.querySelector("#firstname").value;
value.length; // 10

使用jQuery:

代码语言:javascript
复制
var value = $("#firstname").val();
value.length; // 10

下面是一个有效的示例:

​

代码语言:javascript
复制
function Information(msg) {
  simple.alert(msg, () => {});
}
$("#register").click(function(e) {
          e.preventDefault();
          var sex = 'male';
          var firstname = '';
          var lastname = '';
          var date = '';
          if ($('.container[data-sex="female"] input:checked').val() == 'on') {
               sex = 'female';
          }
          if ($('#firstname').val() !== '' && $('#firstname').val() != null && $('#firstname').val().length >= 20) {
              firstname = $('#firstname').val(); 
          } else {
            if ($('#firstname').val().length < 20) {
               return Information('Invalid firstname');
            } else {
               return Information('Firstname can\'t be empty!');
            }
          }
          if ($('#lastname').val() !== '' && $('#lastname').val() != null && $('#lastname').val().length >= 20) {
               lastname = $('#lastname').val(); 
          } else {
            if ($('#lastname').val().length < 20) {
               return Information('Invalid lastname');
            } else {
              return Information('Lastname can\'t be empty!');
            }
          }
          if ($('#date').val() && (/[0-9]/g).test($('#date').val().replace(/\-/g, ""))) {
            date = $('#date').val(); 
          } else {
            if ($('#date').val() !== '') {
              return Information('Date can\'t be empty!');
            } else {
              return Information('Please enter a date');
            }
          }
          $(this).fadeOut(300);
          simple.alert("Successfully registered!");
          $.post('https://lion-register/CreateCharacter', JSON.stringify({
               firstname : firstname,
               lastname : lastname,
               sex : sex,
               date : date,
               queue : MX.CurrentCharacter
          }));
     });
代码语言:javascript
复制
::selection{background-color:#333;color:#f9f9f9}input:-webkit-autofill{-webkit-box-shadow:0 0 0px 1000px white inset}body{caret-color:#333;-webkit-tap-highlight-color:transparent;font-family:Arial,Helvetica,sans-serif}*{box-sizing:border-box}input[type=text],input[type=date],input[type=password]{width:100%;padding:15px;margin:5px 0 22px 0;display:inline-block;border:none;background:#f1f1f1}input[type=password]:focus,input[type=text]:focus{background-color:#ddd;outline:0}button{background-color:#333;color:#fff;padding:14px 20px;margin:8px 0;border:none;cursor:pointer;width:100%;opacity:.9;border-radius:2px}button:hover{opacity:1}.cancelbtn{padding:14px 20px;background-color:#f44336}#register{float:center;width:100%;position:relative;left:50%;margin-left:-50%}.container{padding:16px}.modal{display:block;position:fixed;z-index:1;left:0;top:0;width:100%;height:100%;overflow:auto;background-color:transparent;padding-top:50px}.modal-content{background-color:#fefefe;margin:5% auto 15% auto;width:90%}hr{border:1px solid #f1f1f1;margin-bottom:25px}.clearfix::after{content:"";clear:both;display:table}@media screen and (max-width:300px){#register{width:100%}}
代码语言:javascript
复制
<!DOCTYPE html>
<html>
  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/gh/Parking-Master/Simple-Alert@latest/simplealert.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Register</title>
  </head>
<body>
<div class="modal">
  <form class="modal-content" action="" onsubmit="event.preventDefault();">
    <div class="container">
    <h1 style="text-align:center;font-style:normal;font-family:'Verdana', Verdana, 'Lato', 'Roboto', 'Open sans', Arial, sans-serif;">Register</h1>
      <div style="color:#333;text-align:center;"><a style="text-decoration:none;color:#333;border-bottom:0.006vw solid #333;" href="/help.html">Help</a>&nbsp;&nbsp;&bullet;&nbsp;&nbsp;<a style="text-decoration:none;color:#333;border-bottom:0.006vw solid #333;" href="#">Login instead</a></div>
    
      <hr>
      <label for="email"><b>First name</b></label>
      <input autocorrect="off" spellcheck="false" autocomplete="off" id="firstname" type="text" placeholder="Enter firstname" name="firstname" required>
      
      <label for="email"><b>Last name</b></label>
      <input autocorrect="off" spellcheck="false" autocomplete="off" id="lastname" type="text" placeholder="Enter lastname" name="lastname" required>
      
      <label for="psw-repeat"><b>Date</b></label>
      <input autocorrect="off" spellcheck="false" autocomplete="off" type="date" placeholder="Enter date" id="date" name="date" required>
      
      <label>
        <input type="checkbox" checked="checked" name="remember" style="margin-bottom:15px"> Remember me
      </label>

      <p>By creating an account you agree to our <a href="#" style="color:#333">Terms & Privacy</a>.</p>
      <hr>
      <div class="clearfix">
        <button type="submit" id="register">Register</button>
      </div>
    </div>
  </form>
</div>
</body>
</html>

​

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69866176

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档