Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Social Security number validation with JQuery

Posted on 2011-09-07
8
909 Views
Last Modified: 2013-11-20
Hi, I need to add a method to the JQuery validation plugin. I have this piece of javascript code that I want to add into my JQuery validation. How do I do that?

 
<html>
<head>
<title>Check Norwegian NIN</title>
  <script type="text/javascript">
function checkNIN(o) {
	var nin = o.value;
	if (nin.length != 11) {
		return "must be 11 chars";
	}
	var cc0 = '0'.charAt(0);
	var cw1 = [3, 7, 6, 1, 8, 9, 4, 5, 2];
	var cw2 = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2];
	// check digit 1
	var ws1 = 0;
	for (var n=0; n<cw1.length; n++) {
		ws1 += cw1[n] * (nin.charAt(n) - cc0);
	}
	var cd1 = 11 - (ws1 % 11);
	if (cd1 == 10) {
		return "invalid number, check digit 1 would be 10";
	}
	if (cd1 == 11) {
		cd1 = 0;
	}
	if (nin.charAt(cw1.length) - cc0 != cd1) {
		return "check digit 1 should be " + cd1;
	}
	// check digit 2
	var ws2 = 0;
	for (var n=0; n<cw2.length; n++) {
		ws2 += cw2[n] * (nin.charAt(n) - cc0);
	}
	var cd2 = 11 - (ws2 % 11);
	if (cd2 == 10) { // assuming this is needed
		return "invalid number, check digit 2 would be 10";
	}
	if (cd2 == 11) {
		cd2 = 0;
	}
	if (nin.charAt(cw2.length) - cc0 != cd2) {
		return "check digit 2 should be " + cd2;
	}

	// check age
	var century = -1, numyear = Number(nin.substr(4,2)), numindiv = Number(nin.substr(6,3));
	if (numindiv > 499) {
		if (numindiv < 750 && numyear >= 54) {
			century = 18;
		} else if (numyear < 40) {
			century = 20;
		} else if (numindiv = 900 && numyear >= 40) { // special cases
			century = 19;
		}
	} else {
		century = 19;
	}

	if (century == -1) {
		return "invalid combination of year and individual number";
	}

	var check18 = new Date(century * 100 + numyear + 18, Number(nin.substr(2,2))-1, Number(nin.substr(0,2)), 0, 0, 0, 0);
	alert (check18);
	if (check18 > new Date()) {
		return "Sorry, you have to be at least 18 to get registered";
	}

	return "ok!";
} 
</script>
</head>
<body>
<form>
<input type="text" name="nin" maxlength="11" size="11" onkeyup="document.forms[0]['st'].value = checkNIN(this)">
<br><br>
<input type="text" name="st" readonly size="30">
</form>
</body>
</html> 

Open in new window

0
Comment
Question by:Deenee
  • 3
  • 2
8 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 36493932
check this code

<html>
<head>
<title>Check Norwegian NIN</title>
	<script src="jquery-1.4.2.min.js"></script>

	<script type="text/javascript">
		$(document).ready(function(){
			$("#nin").bind("keyup", function(){
				$("#st").val($(this).checkNIN());	
			});  
		});
		(function($){
			$.fn.checkNIN = function() 
			{	 
				var nin = $(this).val();
				if (nin.length != 11) {
					return "must be 11 chars";
				}
				var cc0 = '0'.charAt(0);
				var cw1 = [3, 7, 6, 1, 8, 9, 4, 5, 2];
				var cw2 = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2];
				// check digit 1
				var ws1 = 0;
				for (var n=0; n<cw1.length; n++) {
					ws1 += cw1[n] * (nin.charAt(n) - cc0);
				}
				var cd1 = 11 - (ws1 % 11);
				if (cd1 == 10) {
					return "invalid number, check digit 1 would be 10";
				}
				if (cd1 == 11) {
					cd1 = 0;
				}
				if (nin.charAt(cw1.length) - cc0 != cd1) {
					return "check digit 1 should be " + cd1;
				}
				// check digit 2
				var ws2 = 0;
				for (var n=0; n<cw2.length; n++) {
					ws2 += cw2[n] * (nin.charAt(n) - cc0);
				}
				var cd2 = 11 - (ws2 % 11);
				if (cd2 == 10) { // assuming this is needed
					return "invalid number, check digit 2 would be 10";
				}
				if (cd2 == 11) {
					cd2 = 0;
				}
				if (nin.charAt(cw2.length) - cc0 != cd2) {
					return "check digit 2 should be " + cd2;
				}

				// check age
				var century = -1, numyear = Number(nin.substr(4,2)), numindiv = Number(nin.substr(6,3));
				if (numindiv > 499) {
					if (numindiv < 750 && numyear >= 54) {
						century = 18;
					} else if (numyear < 40) {
						century = 20;
					} else if (numindiv = 900 && numyear >= 40) { // special cases
						century = 19;
					}
				} else {
					century = 19;
				}

				if (century == -1) {
					return "invalid combination of year and individual number";
				}

				var check18 = new Date(century * 100 + numyear + 18, Number(nin.substr(2,2))-1, Number(nin.substr(0,2)), 0, 0, 0, 0);
				alert (check18);
				if (check18 > new Date()) {
					return "Sorry, you have to be at least 18 to get registered";
				}

				return "ok!";
			};
		})( jQuery );
	</script>
</head>
<body>
<form>
<input type="text" name="nin" id="nin" maxlength="11" size="11">
<br><br>
<input type="text" id="st" name="st" readonly size="30">
</form>
</body>
</html>

Open in new window

0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 36493935
line number 12-79 can be placed in a separate file and include that as plugin
0
 

Author Comment

by:Deenee
ID: 36493962
Hi. I want the code to work together with the validation plugin, because I have a range og other input elements in the form that already use the Jquery validator. I thought I would write it this way, and include the "jQuery.validator.addMethod"  sentence:
 $ (document). ready (function () {
         $ ("# formid"). validate ();
     });

Open in new window

0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:Deenee
ID: 36493965
Sorry, I did not see your first post..
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 36493969
For that you don't need plugin, just create a addMethod like this
http://stackoverflow.com/questions/241145/jquery-validate-plugin-how-to-create-a-simple-custom-rule

0
 
LVL 17

Accepted Solution

by:
sonawanekiran earned 500 total points
ID: 36494033
You don't need to do anything. You just need to call your function from jquery ready function.
See below link to test below code.
http://jsfiddle.net/jGvSz/
<!DOCTYPE html>
<html>
<head>
<title>Kiran Test</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
 $(document).ready(function(){
  $("#nin").keyup(function(){
    $("#st").val(checkNIN(this));
  });
});

function checkNIN(o) {
  var nin = o.value;
  if (nin.length != 11) {
    return "must be 11 chars";
  }
  var cc0 = '0'.charAt(0);
  var cw1 = [3, 7, 6, 1, 8, 9, 4, 5, 2];
  var cw2 = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2];
  // check digit 1
  var ws1 = 0;
  for (var n=0; n<cw1.length; n++) {
    ws1 += cw1[n] * (nin.charAt(n) - cc0);
  }
  var cd1 = 11 - (ws1 % 11);
  if (cd1 == 10) {
    return "invalid number, check digit 1 would be 10";
  }
  if (cd1 == 11) {
    cd1 = 0;
  }
  if (nin.charAt(cw1.length) - cc0 != cd1) {
    return "check digit 1 should be " + cd1;
  }
  // check digit 2
  var ws2 = 0;
  for (var n=0; n<cw2.length; n++) {
    ws2 += cw2[n] * (nin.charAt(n) - cc0);
  }
  var cd2 = 11 - (ws2 % 11);
  if (cd2 == 10) { // assuming this is needed
    return "invalid number, check digit 2 would be 10";
  }
  if (cd2 == 11) {
    cd2 = 0;
  }
  if (nin.charAt(cw2.length) - cc0 != cd2) {
    return "check digit 2 should be " + cd2;
  }

  // check age
  var century = -1, numyear = Number(nin.substr(4,2)), numindiv = Number(nin.substr(6,3));
  if (numindiv > 499) {
    if (numindiv < 750 && numyear >= 54) {
      century = 18;
    } else if (numyear < 40) {
      century = 20;
    } else if (numindiv = 900 && numyear >= 40) { // special cases
      century = 19;
    }
  } else {
    century = 19;
  }

  if (century == -1) {
    return "invalid combination of year and individual number";
  }

  var check18 = new Date(century * 100 + numyear + 18, Number(nin.substr(2,2))-1, Number(nin.substr(0,2)), 0, 0, 0, 0);
  alert (check18);
  if (check18 > new Date()) {
    return "Sorry, you have to be at least 18 to get registered";
  }

  return "ok!";
} 

</script>
</head>
<body>
  <form>
<input type="text" name="nin" id="nin" maxlength="11" size="11">
<br><br>
<input type="text" name="st" id="st" readonly size="30">
</form>
</body>
</html>

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question