manju70
asked on
validating a text field!!!
hai all,
I have textbox for user to key in product_id, I wanted to check the following while keyin itself(it is a alphanumeric value)
1)first two characters should be alphabets.
2)remaining should b numeric.
3)user should not key in beyond 13 characters.
how am I go about it? can any one feed back to me.
I have textbox for user to key in product_id, I wanted to check the following while keyin itself(it is a alphanumeric value)
1)first two characters should be alphabets.
2)remaining should b numeric.
3)user should not key in beyond 13 characters.
how am I go about it? can any one feed back to me.
Another one which also works in Netscape:
function checktextbox(tObj)
{
sVal = tObj.value;
sTmp = "";
sAlpha = "abcdefghijklmnopqrstuvwxy z";
sNumeric = "1234567890";
bFalse = false;
for(i=0;i<sVal.length;i++)
{
if(i<2 && sAlpha.indexOf(sVal.charAt (i))<0)
bFalse = true;
else
sTmp += sVal.charAt(i);
if(i>=2 && sNumeric.indexOf(sVal.char At(i))<0)
bFalse = true;
else
sTmp += sVal.charAt(i);
}
tObj.value = sTmp;
}
with:
<input type=text onKeyUp="checktextbox(this )" maxlength=13>
function checktextbox(tObj)
{
sVal = tObj.value;
sTmp = "";
sAlpha = "abcdefghijklmnopqrstuvwxy
sNumeric = "1234567890";
bFalse = false;
for(i=0;i<sVal.length;i++)
{
if(i<2 && sAlpha.indexOf(sVal.charAt
bFalse = true;
else
sTmp += sVal.charAt(i);
if(i>=2 && sNumeric.indexOf(sVal.char
bFalse = true;
else
sTmp += sVal.charAt(i);
}
tObj.value = sTmp;
}
with:
<input type=text onKeyUp="checktextbox(this
Can be shorter:
function checktextbox(tObj)
{
sVal = tObj.value;
sTmp = "";
sAlpha = "abcdefghijklmnopqrstuvwxy z";
sNumeric = "1234567890";
bFalse = false;
for(i=0;i<sVal.length;i++)
{
if(i<2 && sAlpha.indexOf(sVal.charAt (i))>=0)
sTmp += sVal.charAt(i);
if(i>=2 && sNumeric.indexOf(sVal.char At(i))>=0)
sTmp += sVal.charAt(i);
}
tObj.value = sTmp;
}
function checktextbox(tObj)
{
sVal = tObj.value;
sTmp = "";
sAlpha = "abcdefghijklmnopqrstuvwxy
sNumeric = "1234567890";
bFalse = false;
for(i=0;i<sVal.length;i++)
{
if(i<2 && sAlpha.indexOf(sVal.charAt
sTmp += sVal.charAt(i);
if(i>=2 && sNumeric.indexOf(sVal.char
sTmp += sVal.charAt(i);
}
tObj.value = sTmp;
}
and this one can be gone:
bFalse = false;
bFalse = false;
ASKER
hai zvonko,
Thank you for ur reply
can u please give me the feed back as for what u check the first 3 if condititons.
actually I tried ur coding , it doesn't accepts key in numbers for first two characters. but can accept keyin alpabets for remaining 11 characters.(actually I require remaining 11 should accept only alphabets)
expecting ur reply.
manju
Thank you for ur reply
can u please give me the feed back as for what u check the first 3 if condititons.
actually I tried ur coding , it doesn't accepts key in numbers for first two characters. but can accept keyin alpabets for remaining 11 characters.(actually I require remaining 11 should accept only alphabets)
expecting ur reply.
manju
ASKER
sorry remaining 11 characters should accept only numbers.
manju
manju
ASKER
sorry remaining 11 characters should accept only numbers.
manju
manju
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Second active alert is a nonsense :)
It should read:
alert('Only 0-9 after first two positions.')
The first commented alert is to test key values you like to add to checking.
Cheers,
zvonko
It should read:
alert('Only 0-9 after first two positions.')
The first commented alert is to test key values you like to add to checking.
Cheers,
zvonko
Thanks for the points manju70 :)
How does my code work for you?
How does my code work for you?
ASKER
yah it works the way I expected.Thanks & hats off.
<html>
<head>
<script>
function checkField(event, thisField) {
thischar = (event.which) ? event.which : event.keyCode;
if(thischar < 32) return true;
if(thischar > 90) return false;
if(thischar < 48) return false;
if(thisField.value.length < 2) {
if(thischar > 64) return true;
alert('Only A-Z in first two positions.')
return false;
}
return true;
}
</script>
</head>
<body>
<form>
<input type=text size=13 maxlength=13 onKeyDown="return checkField(event, this)">
</form>
</body>
</html>
Good luck,
zvonko