1

I have an input text into a form where I write an alphanumeric code, that I must validate. So, after I wrote the text, when I click on the submit button

if there is an error in the text 
    it will be displayed an error message
else 
    there will be a submit and success message

The rules to follow are:

  • The length of code must be 27. Not less, not more.

    AA BB C DDDDD EEEEE FFFFFFFFFFFF

  • AA must be "IT" (uppercase).

  • BB are numbers (even with the initial zero).
  • C is a character (uppercase).
  • DDDDD are numbers (even with the initial zero).
  • EEEEE are numbers (even with the initial zero).
  • FFFFFFFFFFFF are characters and numbers. It's sufficient that the length is 12.

How can I validate the code and write this rules with jQuery?

1
  • Please do not use the jquery-validate tag unless your question is specifically about this plugin. Edited. Thanks. Commented Sep 27, 2015 at 15:25

1 Answer 1

1

I put together a function to help you get where you need to be with your password rules. I will leave it up to you to implement it with your form submission because I am not sure how you plan on implementing that part of your application.

Update: I added code to remove white space from input string before checking the rule conditions.

Reason: If white space is allowed in the input a blank space will count as part of the number checks and return true.

jsfiddle Validate Input with Special Rules

Test HTML:

<div id="results"></div>

Test JavaScript:

/*
The rules to follow are:

The length of code must be 27. Not less, not more.

AA BB C DDDDD EEEEE FFFFFFFFFFFF

AA must be "IT" (uppercase). first 2 characters

BB are numbers (even with the initial zero).  3 & 4 characters
C is a character (uppercase). 5th character
DDDDD are numbers (even with the initial zero). characters 5 - 10 
EEEEE are numbers (even with the initial zero). characters 11 - 15
FFFFFFFFFFFF are characters and numbers. It's sufficient that the length is 12.
characters 16 - 27 above
*/

var input = "IT12C1234567891FFFFFFFFFFFF";

validate(input);


function validate(input){//begin function

     //remove all of the white space from the input
     var input = input.replace(/\s+/g, '');

    //test for length
    var testLength = input.length === 27;

    //2nd and 3rd characters equal IT
    var AA = input.substring(0,2) === "IT";

    //3rd and 4th characters are numbers
    var BB = !isNaN(input.substring(2,4));

    //The 5th character is a non numerical capitalized character
    var C = isNaN(input.split("")[4]) && input.split("")[4] === input.split("")[4].toUpperCase();

    //characters 5 - 10 are numbers
    var DDDDD = !isNaN(input.substring(5,10));

    //characters 11- 15 are numbers
    var EEEEE = !isNaN(input.substring(10,15));

    //characters 16 - 27 can be characters and numbers as long as the length is 12
    var FFFFFFFFFFFF = input.substring(15,27).length === 12;

    //if all the password rules checks return true
    if(testLength && AA && BB && C & DDDDD && EEEEE & FFFFFFFFFFFF){//begin if then


        //do what you need to do here if the password is valid

    }
    else{


        //let the user know the error here


    }


    //display test results
    document.getElementById("results").innerHTML = testLength + "<br>" 
        + AA + "<br>"
        + BB + "<br>"
        + C + "<br>"
        + DDDDD + "<br>"
        + EEEEE + "<br>"
        + FFFFFFFFFFFF + "<br>";  



}//end function
Sign up to request clarification or add additional context in comments.

4 Comments

Not a problem. Check out the update I made. There is another check I put in for you that will save you a lot of headaches later on.
Thank you! I'm trying to remove the white spaces also when I copy and paste. But it's not working. jsfiddle, how can I solve it?
I believe you are looking for something like this jsfiddle.net/larryjoelane/gavua9zb
Not a problem if you need any more help send me a message at bytewarestudios.com/contact

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.