Validate ID number in JavaScript

Fuma

Executive Member
Joined
Jul 9, 2007
Messages
5,365
Reaction score
346
Location
Pretoria
How can I validate a textbox has a valid ID number?

Code:
<script>
function validateform(){
.
.code here
.
}
</script>
<form name="frmTest">
   <input type="text" name="txtID">
</form>
 
I couldn't do it on the client side, so I decided to do it on the server side usin php
Code:
function validateIdNumber($id) {

  $match = preg_match ("!^(\d{2})(\d{2})(\d{2})\d{7}$!", $id, $matches);
  if (!$match) {
  return false;
  }
  
  list (, $year, $month, $day) = $matches;
  
  /**
  * Check that the date is valid
  */
  if (!checkdate($month, $day, $year)) {
  return false;
  }
  
  /**
  * Now Check the control digit
  */
  $d = -1;
  
  $a = 0;
  for($i = 0; $i < 6; $i++) {
  $a += $id{2*$i};
  }
  
  $b = 0;
  for($i = 0; $i < 6; $i++) {
  $b = $b*10 + $id{2*$i+1};
  }
  $b *= 2;
  
  $c = 0;
  do {
  $c += $b % 10;
  $b = $b / 10;
  } while($b > 0);
  
  $c += $a;
  $d = 10 - ($c % 10);
  if($d == 10) $d = 0;
  
  if ($id{strlen($id)-1} == $d) {
  return true;
  }
  
  return false;
}

Thank you for those links.
 
You can do that in JavaScript btw, very easy, Heita, I'll suggest you start getting your head stuck into JavaScript. It really is the future.
 
You can do that in JavaScript btw, very easy, Heita, I'll suggest you start getting your head stuck into JavaScript. It really is the future.

Just too lazy to think on a Friday, or any day.
 
That algorithm isn't foolproof. We used something similar but had to lift the strictness of the algorithm a bit, due to the fact that almost a quarter of our clients' employees were flagged as having invalid ID numbers!
 
Well, according to me, verify the ID number by comparing it to information input by the client. For example:

If they chose Male or Female, the ID number reflects that. If they chose their birthday being on the 5th of September 1959, the ID number reflects that.

So a basic check if it's 16 digits long and to see if the information input by the person and their ID number corresponds would give you a fairly good indication if it's a real ID number.

Too bad our government is so backwards, I'd have an API online if I were them, verifying ID numbers with details like registered name so e-commerce people could utilize it in fraud detection. Or something. It's late. Me sleep
 
You can do that in JavaScript btw, very easy, Heita, I'll suggest you start getting your head stuck into JavaScript. It really is the future.
IMHO he would be better of using a static helper to do the validation using the format classes and then further reuse the helper as a EL function through the correct method signiture that automatically happens when one uses static helpers. My logic here is that the business layer would use the helper to ensure that ID numbers are valid as this seems business logic to me and I would throw an Runtime exception in such a case and clearly document that my business API must be called with valid ID numbers there is nothing to suggest that javascript is the future and there are already more favourable solutions like JSF and Swingwhich may be used with javawebstart.
 
IMHO he would be better of using a static helper to do the validation using the format classes and then further reuse the helper as a EL function through the correct method signiture that automatically happens when one uses static helpers. My logic here is that the business layer would use the helper to ensure that ID numbers are valid as this seems business logic to me and I would throw an Runtime exception in such a case and clearly document that my business API must be called with valid ID numbers there is nothing to suggest that javascript is the future and there are already more favourable solutions like JSF and Swingwhich may be used with javawebstart.

WTF? You smoking meth? Why would you want to over complicate a simple client side checking with an API and several layers of doo-hickeys which he barely understands to begin with?

JavaScript is the future, if you pull your head out of your ass you would see how much effort Google is putting into Chrome to make it the fastest JS renderer yet. Seeing all the nice jquery (and other library) stuff you can do, CSS and all the nifty grids you can do client side, why the hell would you NOT think that NOTHING suggests that javascript is the future. Stop wasting my time n00b
 
Seeing all the nice jquery (and other library) stuff you can do, CSS and all the nifty grids you can do client side, why the hell would you NOT think that NOTHING suggests that javascript is the future. Stop wasting my time n00b

So you've seen the light?
 
you can also check here for a whole bunch of local regex strings (phones, credit cards etc)
http://regexlib.com/Search.aspx?k=south+africa

i presume you've seen this pic before (original id book with race codes):
http://z.about.com/d/africanhistory/1/0/1/8/IdentityNumber.gif

my two personal favourite regex links:
http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/
http://www.gskinner.com/RegExr/desktop/ (with built-in templates and realtime checking)

because php has the date / time functions and better string comparison, i find it generally better than js for validation.
at the moment, i do all compulsory validation in ajax and then reuse the same validation object on page submission.
it's much leaner client-side and leads to less overall redundancy.

i love regex.
 
Last edited:
WTF? You smoking meth? Why would you want to over complicate a simple client side checking with an API and several layers of doo-hickeys which he barely understands to begin with?

JavaScript is the future, if you pull your head out of your ass you would see how much effort Google is putting into Chrome to make it the fastest JS renderer yet. Seeing all the nice jquery (and other library) stuff you can do, CSS and all the nifty grids you can do client side, why the hell would you NOT think that NOTHING suggests that javascript is the future. Stop wasting my time n00b

I think you are smoking meth. Fact: NOT EVERYONE knows CSS or even HTML. CSS + HTML are tools for web design and do not fall into the category of tools that are required to develop web applications anymore.

Javascript is just one of the MANY alternatives we have when doing web applications and it is a matter of what you prefer. JSP's do very much have a future with netbooks and all but they are by no means more useful than launching a Swing application through the browser(java webstart) or JSF which is anyway replacing JSP as the industry standard. I can give you a massive reason why the OP should favour JSF over JSP and that is MVC. Not that you cant do MVC with JSP but it's more of a given and easier to do MVC with JSF.

Can you explain why JSP would still be the choice if you already have a swing application? I see some level of rework possibly to implement MVC which is not required with webstart so why would you spend time redoing your code to implemet MVC when you no longer need to for a SWING application that can be deployed as is?


I would say use JSF if you know it (if not now is a good time to learn it.
Use JSP if you know it but learn JSF if you don't.
Don't do anything if you already have a Swing view and just explore java webstart.

I don't see anyone that sticks out above other than learning the newest of the technologies which do not happen to be JSP/servlets.

http://www.coderanch.com/t/210694/JSF/java/JSP-vs-JSF
 
Last edited:
I think you are smoking meth. Fact: NOT EVERYONE knows CSS or even HTML. CSS + HTML are tools for web design and do not fall into the category of tools that are required to develop web applications anymore.

Sorry but I couldn't read past that point highlighted in bold. What utter idiocity
 
Can you explain why JSP would still be the choice if you already have a swing application?

W3C Standards - nuff said

Not everyone runs Java or has Java installed to run it. And having someone require Java just to be able to visit a website is a bit retarded. HTML/CSS and JavaScript. That's it. Chrome rewrote the entire JavaScript rendering engine making it 10x faster (if not more) than normal engines we currently know. AJAX = JavaScript. jQuery = JavaScript (that makes manipulating the DOM so much easier on the client side)

Why would I want an application that needs to call the server every click the user makes? Let me guess, you're the one responsible for the historical computicket system? I have several people here agreeing with me. Together we have over 30 years of experience as web developers, with new and old technologies.... yet you spew this uninformed bull**** in these forums? Eish my bru.... Eish...
 
lol, beat me to it :D
Such a classic... How can ANYBODY forget it? :D

I think you are smoking meth. Fact: NOT EVERYONE knows CSS or even HTML. CSS + HTML are tools for web design and do not fall into the category of tools that are required to develop web applications anymore.
Sorry but I couldn't read past that point highlighted in bold. What utter idiocity
Wow - I wholeheartedly agree. That is UTTER ignorance. The web is presented by HTML to every single client that accesses it.

...'cept when using Flash, Silverlight or other similar client apps. But that sucks. 'nuff said.
 
AcidRaZor... ONE... Yucca... ZERO...


Round 2.... FIGHT!

:p
 
No-one said it was a good idea, even that there is a fool-proof algorithm for testing ID numbers. :) Fact is, there are deviations from the standard algorithm which you only pick up when you deal with employees' demographics from all walks of life.
 
Top
Sign up to the MyBroadband newsletter
X