Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added NIF, NIE and CIF Spanish documents numbers validation #830

Closed

Conversation

alfonsomartinde
Copy link
Contributor

  • Added some translations to /localization
  • Added test suite

Sorry, i forgot to create a new branch for this PR :(

* Added some translations to /localization
* Added test suite

return false;

}, "Por favor, escribe un CIF válido.");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default message should be in English (even though it is a non-English country specific rule ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! 👍

* Changed the default message to English

if (/^[ABCDEFGHJNPQRSUVW]{1}/.test(CIF)){
CIF = n + '';
if (parseInt(num[8],10) === parseInt(String.fromCharCode(64 + n),10) || parseInt(num[8],10) === parseInt(CIF.substring(CIF.length-1, CIF.length),10)){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is evaluating to a return on both branches, it can be replaced with

return (num[8] === String.fromCharCode(64 + n) || num[8] === CIF.substring(CIF.length-1, CIF.length));

Removing the parseInts makes sense since you're actually comparing 2 strings converted to ints.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. Done! Thanks!!

Alfonso Martín added 4 commits July 29, 2013 20:59
* Fixed bug: Changed to string the strict comparison for num[8].
* Fixed: Some comments translated to english.
* Optimized return in cifES.js
* Optimized returns in nifES.js
* Optimized returns in nieES.js
@nschonni
Copy link
Collaborator

I took a crack at refactoring cifES.js here https://gist.github.com/nschonni/bf337e73e57916d88bf5

Could you also switch the code comments to English?

* cifES.js refactorized by 'nschonni'
@alfonsomartinde
Copy link
Contributor Author

Thanks for optimization!! 👍

pos = pos.replace('Y','1');
pos = pos.replace('Z','2');
pos = pos.substring(0, 8) % 23;
return (NIE[8] === cadenadni.substring(pos, pos + 1));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cadenadni.substring(pos, pos + 1) can be replace with charAt(pos)

Alfonso Martín added 2 commits July 29, 2013 23:01
* Removed unused variables.
* Refactorized.
* As suggested by 'nschonni', cadenadni.substring(pos, pos + 1) replaced with charAt(pos)

//XYZ
if (/^[XYZ]{1}/.test(NIE)){
pos = NIE.replace('X','0')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not quite sure what this is doing? Some sort of hashing of the number with corrisponds to the a digit in the eight position? Could you add a comment and then inline and remove the pos variable.

* Translated some variables to English.
* Applied jQuery style guide.
"use strict";

var pos = '',
NIE = value.toUpperCase(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May as well remove the NIE with value = value.toUpperCase() and replace the occurrences below.

@nschonni
Copy link
Collaborator

Sorry for all the nitpicky stuff 👅


@jzaefferer sorry for the noise. I guess we really do need to chat about how this is going to work 😄

* 'n' variable changed to 'controlDigit'.
* Fixed some code styling: spaces after comas, and spaces before curly braces.
* Removed NIF, NIE and CIF variables with value = value.toUpperCase().
* Inlined just one use variables (cadenadni, and some others).
* Removed unused variables.
* Some substring() replaced with charAt().
* Comments translated to English.
@alfonsomartinde
Copy link
Contributor Author

Thanks Nick! :) Updated!

jQuery.validator.addMethod( "nieES", function ( value, element ) {
"use strict";

var position = value.toUpperCase()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be inlined to line 25 since it only needs to be calculated on the one if branch.

Alfonso Martín added 2 commits July 30, 2013 00:10
* Removed 'position' variable: inlined to last test()
* Fixed indentation at nieES.js
@nschonni
Copy link
Collaborator

@jzaefferer is there a specific format for the leading comment block, so it can be parsed to the docs site?

@nschonni nschonni closed this in 317c20f Aug 23, 2013
@nschonni
Copy link
Collaborator

Thanks @alfonsomartinde! squashed it down with a few more comments and landed in 317c20f

@alfonsomartinde
Copy link
Contributor Author

Thank you @nschonni 👍

@nschonni
Copy link
Collaborator

PS: I looks like the email address that you used for the commits isn't registered with GitHub, so it doesn't link to your profile. You can see https://help.github.com/articles/why-are-my-commits-linked-to-the-wrong-user#attach-the-email-to-your-github-account for "fixing" it if you care.

@alfonsomartinde
Copy link
Contributor Author

OMG... Fixed! Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants