Deconstructing some Form Validation Functionality...

I'm breaking down some code that I didn't write so I can better understand it and JQuery in general.

Here's the form validation function...

function validateForm() {
	var dob = document.getElementById("usrDOB");
	var usrSSN = document.getElementById("usrSSN");
	var error = '';
	if (!isValidDate(dob.value)) 
	{
		$('#usrDOB').css('border', 'solid 2px red').focus();
		error = "Date of Birth must be in the format XX/XX/XXXX\n\r";
	} 
	else 
	{
		$('#usrDOB').css('border', '');
	}
	
	[b]if (!isInt(usrSSN.value)) [/b]
	{
		$('#usrSSN').css('border', 'solid 2px red').focus();
		error = error.concat("SSN must be numeric\n\r");
	}
	else
	{
		$('#usrSSN').css('border', '');
	}
	
	if (error.length > 0) {
		alert(error);
		return false;
	}
	return true;
}

Open in new window


My question is with "isInt."

Here's that function:

function isInt(value) {
    	return !isNaN(parseInt(value, 10))
			&& (parseFloat(value, 10) == parseInt(value, 10));
}

Open in new window


I saw from some "googling," that "isNan" is a built in JQuery function. I'm just trying to figure out the logic of what's there.

 return !isNaN(parseInt(value, 10))
&& (parseFloat(value, 10) == parseInt(value, 10));

I want to be able to explain to someone in plain English what's happening and I'm stumbling.

"parseInt" is going to trim any superflous text from the value that's being passed into the function and focus on the integer that's present (https://www.w3schools.com/jsref/jsref_parseint.asp). But what's "10?"

parseFloat is looking at a number and reducing it to a prescribed number of decimal places.

So, going back to the original function, it looks as though it's wanting an equality to exist between what's being parsed and what's being floated, but I don't understand "10" and it looks as though the function is set up to return a "false" value.

What is this doing?
brucegustPHP DeveloperAsked:
Who is Participating?
 
Eddie ShipmanAll-around developerCommented:
Basically it is saying:

If this is Not (not parsed as an int) OR the value is parsed as a float equals the value parsed as an integer, then return true.
0
 
Eddie ShipmanAll-around developerCommented:
The "10" is the radix. According to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt:

An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the above mentioned string. Specify 10 for the decimal numeral system commonly used by humans. Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified, usually defaulting the value to 10.
0
 
brucegustPHP DeveloperAuthor Commented:
Eddie!

I get it, now! And the radix is important because if you're using a binary value, then the radix is going to be different so that's why you want to specify it.

Going back to the function...

return !isNaN(parseInt(value, 10)) && (parseFloat(value, 10) == parseInt(value, 10));

With the "10" mystery solved, I'm still scratching my head as to what is being done / asked.

What's being "returned?"

It looks as though you've got a value...

NaN is looking to see if it's an integer...

And then what?

Thanks!
0
 
brucegustPHP DeveloperAuthor Commented:
Got it!

Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.