...

View Full Version : JS restrictive validation - decimal



JohnShell
08-26-2010, 04:15 PM
Hi,
Dreamweaver generated the javascript for an onBlur validation for an input textbox. Is it possible to change this code so that I do not have a range but, instead, specific numbers that must be input by the user? I want to ensure that the users input integers and only the following decimals: .25; .5, and
.75.

[function MM_validateForm() { //v4.0
if (document.getElementById){
var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
if (val) { nm=val.name; if ((val=val.value)!="") {
if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
} else if (test!='R') { num = parseFloat(val);
if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
min=test.substring(8,p); max=test.substring(p+1);
if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
} } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
} if (errors) alert('The following error occurred:\n'+errors);
document.MM_returnValue = (errors == '');
} }
</script>]

John

Old Pedant
08-26-2010, 07:15 PM
DrunkWalker creates some of the most gawdawful JS code you'll ever see.

That email validation that is part of the code you show is, for example, so inadequate it is worthless. Ditto its validation of text fields. And, as you noted, its validation of numbers is nearly pointless. Doesn't even allow you to insist on integers, for example.

It's way way past time to get rid of DreadedWeasel code whenever possible.

Yes, you can easily build JavaScript to validate most anything you can think of.

Example to meet your specs:


<html>
<head>
<script type="text/javascript">
function checkQuarters( fld )
{
var val = parseFloat(fld.value);
if ( isNaN(val) || ( val*4 != Math.floor(val*4) ) )
{
alert("Value must be a multiple of 0.25");
return false;
}
return true;
}
</script>
</head>
<body>
<form>
Amount, to nearest 25 cents:
<input name="whatever" onchange="checkQuarters(this);" />
</form>
</body>
</html>

prasanthmj
08-27-2010, 05:00 AM
I want to ensure that the users input integers and only the following decimals: .25; .5, and .75.

Why not make it a dropdown?

Typing in some value and then the validation saying that the 'value is not good ' makes it frustrating.

JohnShell
08-27-2010, 06:47 PM
Perfect! Thank you very much. BTW - you had me with DrunkWalker until I saw DreadedWeasel and realized you meant DreamWeaver - LOL on that one.
Again, thanks - works great.

JohnShell
08-27-2010, 06:49 PM
I appreciate your suggestion but, it would encompass too many possible selections in this instance. Old Pendant's code works perfectly for my needs. Again, thank you.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum