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?
Bruce GustPHP DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Bruce GustPHP 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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bruce GustPHP DeveloperAuthor Commented:
Got it!

Thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.