PDA

View Full Version : Find the problem ???


krnaveen
09-10-2002, 10:23 AM
Hi!,
Am using the function as below to check for the numeric value as entered by the user in the text box on an HTML form.

function isNumeric(txtValue)
{
var num = new Array(0,1,2,3,4,5,6,7,8,9);
for(i=0;i<txtValue.length;i++)
{
chr = txtValue.charAt(i);
for(j=0;j<num.length;j++)
{
if(chr == num[j]);
return false;
else
return true;
}
}
}

function numericCheck()
{
if(isNumeric(document.Amount_Arrivals.diamond.value))
{
alert("Enter Numbers only");
document.Amount_Arrivals.diamond.value=""
document.Amount_Arrivals.diamond.focus();
return false;
}
}


Am calling the function on the onChange of the textfield which is as below:
<input type="text" name="diamond" size="20" tabindex="3" onChange="return numericCheck()">
Can some of you friends please verify the code and help me out.
Help needed urgently.

-Navin.

realisis
09-10-2002, 12:17 PM
krnaveen,

Here try this. I think it should do the trick. Shorter too!

There's only the one function with validation included therein. Also changed the event-handler from onChange to onBlur...

<input type="text" name="diamond" size="20" tabindex="3" onBlur="return numericCheck()">

function numericCheck()
{
usernum = document.Amount_Arrivals.diamond

rx = /^\d+$/
//validation is the rx var

if ((usernum.value != "") && !rx.test(usernum.value))
{
alert("Enter Numbers only")
usernum.value=""
usernum.focus()
return false
}
}

Instead of forcing usernum.value="" after the alert, you might consider using simply usernum.select() instead, and then the usernum.focus().

This leaves the field with any invalid non-digits, but it doesn't force the user to re-type a whole string if only the one character was wrong. They'll just need to correct that one character. Plus it allows them to confirm that there actually was a non-digit, rather than staring at a blank field, and wondering "did I really...?"

onBlur locks them into the field anyway until they provide a valid input, or none at all (unlike onChange, which only catches it the *first* time around).


Or you could use this alert:

alert("You typed in:\n\n" +usernum.value+ "\n\nPlease use numbers only")

...

Personally both "return" statements seem superfluous to me and I would omit them, but i'm not sure what else your script does, so these may be necessary, in context.

Hope this helps.

Ökii
09-10-2002, 01:08 PM
or...

<input type="text" name="digits" onkeyup="if(this.value*1!=this.value)this.value=this.value.substr(0,this.value.length-1);">

which simply removes anything that isn't a number or a decimal point.

realisis
09-11-2002, 10:37 AM
Okii,

Ah, that hadn't really occured to me. Could achieve the same by asking if(isNaN(this.value))

I experimented with it a bit. Doesn't catch white-space, or when you type characters into the middle of the string, and it pretty much falls apart when pasting text... So on its own it's got limited application I suppose...

All the same, it's a cool idea. Thanks for the tip.

piglet
09-11-2002, 10:51 AM
Hi All,

It's amazing how similar the question on this forum is to this question! (http://www.webxpertz.net/forums/showthread.php3?s=&threadid=18963)

hehe

realisis
09-11-2002, 11:10 AM
ah yes. And I notice he hasn't yet acknowledged his respondents on that thread either.

Some good answers there. Thanks for the resource and the link piglet.

Ökii
09-11-2002, 01:33 PM
If you ran a lil function on both onkeyup and onblur, you could get a step further toward an effective method.

<form name="myform">
<input type="text" name="digits" onkeyup="digitsonly('myform','digits');" onblur="digitsonly('myform','digits');">
</form>

<script language="javascript">
function digitsonly(fm,fld) {
dval = document.forms[fm].elements[fld].value;
if(dval.indexOf(' ')>-1) {
dval = dval.substr(0,dval.indexOf(' '));
document.forms[fm].elements[fld].value = dval;
}
if(dval*1!=dval) {
document.forms[fm].elements[fld].value = dval.substr(0,dval.length-1);
digitsonly(fm,fld);
}
}
</script>

which should only allow numerics, remove whitespace and rebuild the field value to include the last proper digit entered. You can though sneak in an put a minus sign in front of numbers. That's a bit of a bulkier script though :D

RadarBob
09-11-2002, 01:35 PM
However the final solution takes shape I suggest you do not automatically delete what the user typed. The user needs to see what (s)he typed.

There is a previous there here where the coder was trying to keep the user from entering the same mistake over and over. Well, if you tell the user *clearly* what the error is, then leave alone what (s)he typed, then the user can see what/where the error is.

piglet
09-11-2002, 02:10 PM
Hi All,

This has got to be about as simple as you can do it - the original question was for positive integers only - you get told what the input was, and it replaces it with valid data:


<HTML><BODY>
<form name="myform">
<input type="text" name="digits" onkeyup="ck(this)" >
</form>
<SCRIPT LANGUAGE="JavaScript">
<!--
function ck(t){
var n=parseInt(t.value)+''
if (n!=t.value)alert("'"+t.value + "' is not a valid positive integer")
t.value=(n=='NaN')?'':n
}
//-->
</SCRIPT></BODY></HTML>