Click to See Complete Forum and Search --> : textfield validation problem
srikanthrl
08-08-2003, 07:55 AM
hi,
this is my function i have to get the text field name and make the focus on the respective textfield when the condition meets. i am facing the prob when making to focus.
can anybody help . thanks in advance
function compare(myobj)
{
var xx = myobj.name;
var enteredVal=myobj.value;
if(enteredVal == ""){
alert("field cannot be empty");
document.npvForm.xx.focus();
return false;}
}
//calling the function at the event onchange like this
<input type=TEXT name="first" value='0' onchange="compare(this)" >
Khalid Ali
08-08-2003, 08:05 AM
instead of this line
document.npvForm.xx.focus();
use this line
myobj.focus();
srikanthrl
08-10-2003, 11:58 PM
hi,
my code is something like this. i will have nearly 30 to 40 text fields to validate. in this code i have given only 5 fields.
i have to allow numbers and decimal point(that is working fine) but i should not allow the field empty i.e., deleting the default value. pl.. help how to solve this either by changing the code or by giving some other solution.
thanks in advance.
regards,
srikanth
<HTML>
<HEAD>
<script>
var isIE = document.all?true:false;
var isNS = document.layers?true:false;
function onlyDigits(e,decReq,tex) {
var key = (isIE) ? window.event.keyCode : e.which;
var obj = (isIE) ? event.srcElement : e.target;
var isNum = (key > 47 && key < 58 ) ? true:false;
if(isNum == false && key != 46) alert("Only Numbers Allowed");
var dotOK = (key==46 && decReq=='decOK' && (obj.value.indexOf(".")<0 || obj.value.length==0)) ? true:false;
window.event.keyCode = (!isNum && !dotOK && isIE) ? 0:key;
e.which = (!isNum && !dotOK && isNS) ? 0:key;
return (isNum || dotOK);
}
function compare(myobj)
{
var enteredVal=myobj.value;
if(enteredVal == ""){
alert("field cannot be empty");
myobj.focus();
return false;}
}
function buttonClick()
{
document.npvForm.submit();
}
}
</SCRIPT>
</Head>
<BODY>
<FORM name=npvForm method=GET action="NPV_calc250703.jsp">
<table align=center>
<tr>
<td>
Enter First no :</TD>
<TD><input type=TEXT name="first" value='0' onchange="compare(this)" onkeypress="onlyDigits(event,'decOK')">
</TD>
</tr>
<tr>
<td>
Enter Second no :</TD>
<TD><input type=TEXT name="second" value='0' onchange="compare(this)" onkeypress="onlyDigits(event,'decOK') ">
</TD>
</tr>
<tr>
<td>
Enter Third no :</TD>
<TD><input type=TEXT name="third" value='0' onchange="compare(this)" onkeypress="onlyDigits(event,'decOK')">
</TD>
</tr>
<tr>
<td>
Enter Fourth no :</TD>
<TD><input type=TEXT name="fourth" value='0' onchange="compare(this)" onkeypress="onlyDigits(event,'decOK')">
</TD>
</tr>
<tr>
<td>
Enter Fifth no :</TD>
<TD><input type=TEXT name="fifth" value='0' onchange="compare(this)" onkeypress="onlyDigits(event,'decOK')">
</TD>
</tr>
<tr>
<td>
<INPUT type=Button value=click onclick="buttonClick()"></TD></TR>
</TABLE>
</FORM>
</BODY>
</HTML>