vendredi 1 décembre 2017

Using JQuery, how do I check that a string is a letter character followed by eight digits?

I'm a beginner writing JQuery to add to a customization form website. Most of the options are drag and drop, but I have to write custom jquery in some cases.

For this, I've been able to figure out to validate a nine-character string so that an error message is presented if the string is NOT 9 characters long, and if it starts with anything other than "B", "E", or "N."

However, it also needs to check and make sure that all other characters after the first is a digit. For instance, an acceptable user input would be e00012345.

What is the simplest way to do this?

// this validation will check to make sure that an accepted value is entered into a field.
// currently, the validation is not perfect. Ideally, the value would start with a specific character (n, b or e) and 8 digits. Right now it just must start with n, b or e and be 9 characters long. 

$(function() {

  // for the Missouri Business Number -- on blur, if the value is 9 characters long and starts with b, e, or n (uppoer or lower case), then the input is valid. Otherwise, error messages appear.

$("input#id_wrT4duNEOW").blur(function() {
    if (
        (($("#id_wrT4duNEOW").val().startsWith("b")) &&
        ($("#id_wrT4duNEOW").val().length == 9)) ||
        (($("#id_wrT4duNEOW").val().startsWith("e")) && 
        ($("#id_wrT4duNEOW").val().length == 9)) || 
        (($("#id_wrT4duNEOW").val().startsWith("n")) && 
        ($("#id_wrT4duNEOW").val().length == 9)) || 
        (($("#id_wrT4duNEOW").val().startsWith("B")) && 
        ($("#id_wrT4duNEOW").val().length == 9)) || 
        (($("#id_wrT4duNEOW").val().startsWith("E")) && 
        ($("#id_wrT4duNEOW").val().length == 9)) || 
        (($("#id_wrT4duNEOW").val().startsWith("N")) && 
        ($("#id_wrT4duNEOW").val().length == 9))
    ) 
    {
      // good things happen
    } 
else {
     // error message
    }
  });
});
<script src="http://ift.tt/1oMJErh"></script>

Aucun commentaire:

Enregistrer un commentaire