Now, I need a very specific validation of a field called 'NIF', wich is the identifier of a tax-paying company in my country. And I need some help to make a regular expression to use like I did in the above code.

So I found an old javascript code online that validates 'NIF':

function IsValidNIF(nif){ var c; var checkDigit = 0; //Check if is not null, is numeric and if has 9 numbers if(nif != null && IsNumeric(nif) && nif.length == 9) { //Get the first number of NIF c = nif.charAt(0); //Check firt number is (1, 2, 5, 6, 8 or 9) if(c == ‘1' || c == ‘2' || c == ‘5' || c == ‘6' || c == ‘8' || c == ‘9') { //Perform CheckDigit calculations checkDigit = c * 9; var i = 0; for(i = 2; i <= 8; i++) { checkDigit += nif.charAt(i-1) * (10-i); } checkDigit = 11 - (checkDigit % 11); //if checkDigit is higher than ten set it to zero if(checkDigit >= 10) checkDigit = 0; //Compare checkDigit with the last number of NIF //If equal the NIF is Valid. if(checkDigit == nif.charAt(8)) return true; } } return false;}function IsNumeric(ObjVal){return /^\d+$/.test(ObjVal);}

So, is it possible to make a regular expression from this?

The rules are:

-must be only numbers
-must be 9 characters(numbers) long
-the first number must be 1 or 2 or 5 or 6 or 8 or 9
-the last number performs some validation(%module) that you should understand viewing the above code

-must be only numbers
-must be 9 characters(numbers) long
-the first number must be 1 or 2 or 5 or 6 or 8 or 9
characterReg = /^[125689][0-9]{8}$/

-the last number performs some validation(%module) that you should understand viewing the above code
This part would produce a more complicated regular expression than is worth it, so I'd stick to using code.

If possible let me know "how" complicated would the regex become. I don't mind if the regex is not "readable", the aproach of regex is just to cut some lines of code. Unless the regex could have some performance/other issues,. is that the case?

I have to agree with ozo: You're going to be attempting to reproduce modulo arithmetic via a regex. This isn't really what regex is designed for. The code approach would be much simpler. Just use ozo's suggested pattern, then add logic for the modulo check.

0

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Then how would I add up the 2 things (regex + code) in my form?

Should be like:

...//some codevar myRegex = /^[125689][0-9]{8}$/;if ( myRegex.test(inputValName) ){ // then implement other if here with the modulo% code??...//more code

but i a working with jquery here, I am more familiar with with. Don't know how to manipulate the DOM with pure js. Can I incorporate that function in my jquery?

function IsValidNIF(nif){ var checkDigit = 0; var i = 0; var characterReg = /^[125689][0-9]{8}$/; if (characterReg.test(nif) { //Perform CheckDigit calculations checkDigit = nif.charAt(0) * 9; for(i = 2; i <= 8; i++) { checkDigit += nif.charAt(i-1) * (10-i); } checkDigit = 11 - (checkDigit % 11); //if checkDigit is higher than ten set it to zero if(checkDigit >= 10) checkDigit = 0; //Compare checkDigit with the last number of NIF //If equal the NIF is Valid. if(checkDigit == nif.charAt(8)) return true; } return false;}

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.

Nothing in an HTTP request can be trusted, including HTTP headers and form data. A form token is a tool that can be used to guard against request forgeries (CSRF). This article shows an improved approach to form tokens, making it more difficult to…

Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

The viewer will learn the basics of jQuery, including how to invoke it on a web page.
Reference your jQuery libraries: (CODE)
Include your new external js/jQuery file: (CODE)
Write your first lines of code to setup your site for jQuery.: (CODE)