简体   繁体   中英

Can't get JavaScript SSN Form Validation to work

I am new to JS and trying to get it so when someone enters a SSN number in a field, it gives them an alert telling them to NOT put a SSN number in there.

HTML:

<form name="card" action="#">
    <input type="text" name="field" class="name social" size="60" placeholder="First and Last Name">
    <input type="text" name="field" class="phone social" size="60" placeholder="Phone Number">
    <input type="text" name="field" class="email social" size="60" placeholder="Email(name@example.com)">

    <select class="select">
        <option value="My card has not yet arrived">My card has not yet arrived
        <option value="Direct Deposit">Direct Deposit
        <option value="Suggest a Card">Suggest a Card
        <option value="Issues with CARD.com">Issues with CARD.com
        <option value="Other">Other
    </select>

    <textarea name="field" class="text social " cols="60" rows="5" placeholder="How can we help you?"></textarea>
    <input type"submit" name="submit" class="subBtn" value="Submit" onclick="warnCC(document.card.field)">Submit</input>
</form>

JS:

<script>
  function warnCC()  
    {  

      var ssn = document.getElementsByTagName("input");
      // document.getElementsByClassName("social");
      var pattern = /^[0-9]{3}\-?[0-9]{2}\-?[0-9]{4}$/;
      if (ssn.value.match(pattern)) 
      {
        return true;
        alert("Please Do Not Enter SSN Info Into This Form");

      }
      else 
      {
        return false;
        alert("yay!")
      } 
    }
</script>

Any help on where I cam going wrong would be very helpful. I'd also prefer it was done on a "onfocusout" if anyone can give me advice on that.

getElementsByTagName and getElementsByClassName both return a list of elements. That list doesn't have a value property. Each of the items on the list has a value property, but not the list.

You'll want to loop through the list of inputs and handle each of them as appropriate, eg:

var list = /*...get the list using either method you have in your code...*/;
var index, input;
for (index = 0; index < list.length; ++index) {
    input = list[index];
    // input.value will be the value of this particular input
}

Side note: querySelectorAll has better support than getElementsByClassName (IE8 has it, for instance, but not getElementsByClassName ), so if you want to get a list using a class, you're best off with:

var list = document.querySelectorAll(".the-class-here");

您还应该在从函数返回之前执行alert()...如果您希望显示alert()对话框。

I tweaked your function. You can see it inaction here: http://jsfiddle.net/tBqP2

  function warnCC() {

      var formElements = document.getElementsByTagName("input");
      for (var k in formElements) {
          // document.getElementsByClassName("social");
          var pattern = /^[0-9]{3}\-?[0-9]{2}\-?[0-9]{4}$/;
          if (formElements[k].value.match(pattern)) {
              alert("Please Do Not Enter SSN Info Into This Form");
              return false;
          }

      }
      return true;
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM