Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

html - I have a javascript that checks if my form is valid and it stops checking after a certain field

So I made a form in a table in html and the javascript code checks till the (creditcard.value.length) after that the code doesn't check anything

<script language="javascript" type="text/javascript">
     function ispsd(form) {
       var passed = false;

       if (form.Fullname.value.length < 4) {
         alert("Enter a valid Full Name");
       } else if (form.Email.value.indexOf("@") == -1) {
         alert("Enter a valid E-mail adress.")
       } else if (form.Email.value.indexOf(".") == -1) {
         alert("Enter a valid E-mail adress.")
       } else if (form.Cardholder.value.length < 3) {
        alert("Card Holder name is not Valid.")
       } else if (form.Creditcard.value.length != 16) {
         alert("Credit card number is not valid.")
       } else if (isNan(form.Creditcard.value)) {
         alert("Credit card number cannot contain letters.")
       } else if (isNan(form.Zip.value)) {
         alert("Enter a valid Postal Code.")
       } else if ((form.Expyear.value) * 1 < 2021) {
         alert("Credit Card has Expired.")
       } else if (isNan(form.Expyear.value)) {
         alert("Enter a valid Year.")
       } else if (form.cvv.value.length != 3) {
         alert("Enter a valid CVV.")
       } else if (isNan(form.cvv.value)) {
         alert("CVV cannot contain letters.")
       } else {
         passed = true;
       }
       return passed;
     }
</script>

and the thing is when I moved the (form.Expyear.value) * 1 < 2021) above the (form.Creditcard.value.length != 16) the validation worked and when I tried to add all the (else if) above the Credit card check it didn't work

don't know what's the problem if anyone can help I would be thankful

question from:https://stackoverflow.com/questions/65882839/i-have-a-javascript-that-checks-if-my-form-is-valid-and-it-stops-checking-after

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  • Try to remove the * 1, not sure what's the purpose there
  • isNaN, and not isNan

I would also handle it differently, what you need is to return true if they pass, rather than identify errors, for example, the demo here below. For example, it will pass your test if you have more than 16 numbers since you're checking x !== 16

function validate() {
  var x, text;

    // Get the value of the input field with id="numb"
    x = document.getElementById("cc").value;

    // If x is Not a Number or less than one or greater than 10
    if (!isNaN(x) && x.length > 3 && x.length <= 16) {
      text = "Input OK";
    } else {
      text = "Input not valid";
    }
    document.getElementById("error").innerHTML = text;
}
<p>Please write only numbers, from 4 to 16 maximum characters</p>
<input type="number" id="cc"/><br>
<span id="error"></span><br>
<input type="submit" onclick="validate()" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...