-2

I have created this:

<form class="form-horizontal" method="post" action="profile.php">
    <div class="form-group">
        <label class="col-sm-2 control-label">Card Number</label>
        <div class="col-sm-7"> 
            <input type="text" id="card_no" class="form-control" name="card_number" maxlength="16" autofocus="autofocus">
        </div>
        <button class="btn btn-primary demo1" type="submit" id="submit" onclick="check()">SEARCH</button>
</div>

When the card's length is greater or lower than 16 an alert message must be displayed. So I tried this:

$('#submit').onclick(function() {
    var inputlength = $('#card_no').val().length;
    if (inputlength<>16) {
        sweetAlert("ERROR", "Card must have 16 digits", "warning");
        return false;
    }
});  

But it doesn't work. I also have an alert when there is NO input and works fine! Any ideas?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
gdim
  • 203
  • 1
  • 4
  • 13

4 Answers4

2

Use if(inputlength !== 16)

$('#submit').on('click',function() {
  var inputlength = $('#card_no').val().length;
  if (inputlength !== 16) {
    sweetAlert("ERROR", "Card must have 16 digits", "warning");
    return false;
  }
});
<link href="https://cdn.jsdelivr.net/sweetalert/1.1.3/sweetalert.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/sweetalert/1.1.3/sweetalert.min.js"></script>
<form class="form-horizontal" method="post" action="profile.php">
  <div class="form-group">
    <label class="col-sm-2 control-label">Card Number</label>
    <div class="col-sm-7">
      <input type="text" id="card_no" class="form-control" name="card_number" maxlength="16" autofocus="autofocus">
    </div>
    <button class="btn btn-primary demo1" type="submit" id="submit">SEARCH</button>
  </div>
</form>
Parvez Rahaman
  • 4,269
  • 3
  • 22
  • 39
0

This should work. Just change alert with your sweetAlert

$('#submit').click(function() {
    var inputlength = $('#card_no').val().length;
    if (inputlength!=16) {
        alert("ERROR", "Card must have 16 digits", "warning");
        return false;
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<form class="form-horizontal" method="post" action="profile.php">
    <div class="form-group">
        <label class="col-sm-2 control-label">Card Number</label>
        <div class="col-sm-7"> 
            <input type="text" id="card_no" class="form-control" name="card_number" maxlength="16" autofocus="autofocus">
        </div>
        <button class="btn btn-primary demo1" type="submit" id="submit">SEARCH</button>
</div>
Neelesh
  • 666
  • 6
  • 17
0

See the example below and make adjustments if necessary. Using != sets it so that when entered value is lesser-than or greater-than the defined value, the alert will be triggered.

$('#submit').on("click", function() {
  
  var $inputlength = $('#card_no').val();
  
  if ($inputlength != 16) {
    
    alert('ERROR, Card must have 16 digits. "warning!"'); 
    
    /* Change "alert" to sweetAlert */
    
    return false;
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="form-horizontal" method="post" action="profile.php">
  <div class="form-group">
    <label class="col-sm-2 control-label">Card Number</label>
    <div class="col-sm-7">
      <input type="text" id="card_no" class="form-control" name="card_number" maxlength="16" autofocus="autofocus">
    </div>

    <button type="submit" id="submit" ">SEARCH</button>
</div>
Sleek Geek
  • 4,638
  • 3
  • 27
  • 42
-1

You need to name your function check() if you want to call the function. Also, the logic operator is not correct, there is a <>, and a < is needed to test the input.

$('#submit').onclick(function check() {

          var inputlength = $('#card_no').val().length;

         if (inputlength<16) {
        alert("ERROR", "Card must have 16 digits", "warning");
                return false;
       }
     }); 
Juan Davila
  • 61
  • 1
  • 11