Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JAVASCRIPT input validation ! Please help !

Status
Not open for further replies.

sammes

Programmer
Jul 13, 2004
7
GB
Hi !
Who could help me with a javascript ?

What I need:

We have pages, where a user could input amounts.
These amounts could only be numbers (e.g. 100) with OR without an "M" (for million) or an "T" (for thousand):

e.g. 100 (hundred) , 100M (one hundred million), 100T (one hundred thousand) and so on.

- I need a validation script (which can be used for onKeyPress and onBlur) which ONLY accepts ".1234567890MT".

- This script has also to check whether the user puts in a "," . If so, the script has to replace this with an "."

- Only ONE "." is allowed, this means, if the user tries to put in: "100.000.000", the script should take out the last ".", because ONLY ONE "." is allowed.

-It should always capitalize the "m" or "t", whgen a user put that in. e.g. : 100m -> 100M

At least, there should be a parameter which thell the function, in the user is allowed to put in a NEGATIVE value or NOT.

e.g.: function_xy(notNegative)...

if notNegatibe == true -> no negative values are allowed, the script should also not allow to put in a "-" before the number, M or T is allowed

if notNegative == false -> the user could input 0-9, negative values are allowed, the user can put in a "-" before the number, M or T is allowed

The first function should be e.g. validOnKeyPress(notNegative);

The second function should be e.g. validFieldOnBlur(notNegative);

I hope, this is not to much :)

Could somebody help me here ?

This would be GREAT !!!

regards
Michael
 
What part of the code (from the previous thread) are you having problems modifying to adjust to these new requirements?

Jeff
 
Hi Jeff !

Thats for another project. The script does not match THIS requirements as I described above.
The script should exactly match these things.

This script should NOT convert "100M" to 100000000 or anything. It should accept the numbers, an "M", an"T".
It should only accept ONE "."
It should replace a "," with an "."

It should handle the "negative or NOT negative number" problem. (not allow "-" before the number) etc.

regards

Michael
 
do a parsing function that analises every figure of the number from left to right

if it's a number, simply add it to the output string, if it's a letter make sure the output string already has a value and that the value does not contain another letter, and that the letter is M or T... and so on.

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
easy to say, not so easy for me...
Thanks.
 
it's not the best you can do but it was made in a hurry. but it should at least give you an idea.

Code:
function evaluateString(strToEvaluate)
{
	var correctString = '';
	var letterFound = false;
	var dotFound = false;
	var isCorrect = true;
	for (i=0; i<strToEvaluate.length; i++)
	{
		if (parseInt(strToEvaluate.charAt(i)))
		{
			correctString += strToEvaluate.charAt(i);
		}
	    	else
		{
			if (strToEvaluate.charAt(i) == 'm' || strToEvaluate.charAt(i) == 'M')
			{
				if (!letterFound)
				{
					correctString += 'M';
					letterFound = true;
				}
				else 
				{
					alert('found two letters');
					isCorrect = false;
					break;
				}
			}
			else if (strToEvaluate.charAt(i) == 't' || strToEvaluate.charAt(i) == 'T')
			{
				if (!letterFound)
				{
					correctString += 'T';
					letterFound = true;
				}
				else
				{
					alert('found two letters');
					isCorrect = false;
					break;
				}
			}
			else if (strToEvaluate.charAt(i) == '.' || strToEvaluate.charAt(i) == ',')
			{
				if (!dotFound)
				{
					correctString += '.';
					dotFound = true;
				}
				else
				{
					alert('found two dots');
					isCorrect = false;
					break;
				}
			}
			else
			{
				alert('character not recognised');
				isCorrect = false;
				break;
			}
		}
	}
	if(isCorrect) alert (correctString);
}

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 

I would have thought that regexp would be the way to go, but unfortunately, I know nothing about regexp, so my only solution would be similar to DaZZleD's.

Maybe someone else will know the regexp for this task?

Hope this helps,
Dan
 
would be something like:

/[0-9]+(\.)?[0-9]*?(t|m)/gi

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top