PDA

View Full Version : Enter an integer


ooiooipig
04-04-2003, 02:18 AM
Hi all, can anyone help me in validating numbers?? I've a field "Contact" which inside the database, i set it as NChar. But now in the page, i want to make sure users enter numbers only. Can anyone advise on how to do it??

Mhtml
04-04-2003, 03:49 AM
I'd do it using IsNumeric().

Syntax:
IsNumeric(expression)


Returns True or False.

miranda
04-04-2003, 10:19 AM
if you are coding for IE only then this will prevent the user from entering any non number
<input type="text" onKeypress="if (event.keyCode< 45 || event.keyCode > 57) event.returnValue = false;">


If you want to cover the bases and do so on all browsers then check on the client like so
<input type="text" onBlur="validateNmbr(this)" onKeypress="if (event.keyCode< 45 || event.keyCode > 57) event.returnValue = false;">

<script>
function validateNmbr(field) {
var valid = "0123456789"
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert("Invalid entry! Only numbers are accepted!");
field.focus();
field.select();
}
}
</script>


yes you can test it on the server using IsNumeric(expression) but you really should validate on the client side if at all possible

Mhtml
04-04-2003, 10:35 AM
That is sort of to be expected. All validation should be done in client side first. And it should be cross browser, not browser specific. Server side validation is always needed as some users may have older browsers, browsers not supporting javascript, or not have javascript turned on.

Also, that is a heck of a lot more coding that you need. RegExp will provide numeric validation in 3 lines of code.

whammy
04-05-2003, 11:44 PM
P.S. No offense intended, Mhtml, but NEVER, EVER use IsNumeric() to validate that the user only entered digits. For this you need a regular expression! ;)

One of my projects in the coming months is to update validation on around 70 websites where another programmer mistakenly did that very thing. :(

I have fixed it in the worst cases, but there is still a lot of work to be done.

Using IsNumeric() apparently (from some of the data I've seen!) also allows entries like:

2de3

+1.2

-12,000

1,000,000+

etc., which is unacceptable data in regards to one of the vendors my company deals with (they only expect numbers in certain fields; any other characters will cause a record to be rejected! They are fixing these cases manually at the moment, but I will eventually get it all corrected programmatically, when I have time...).

Instead, you should not only validate client-side, but server-side as well. Here's an example:


Function IsDigits(str)
Dim idRegEx
Set idRegEx = New RegExp
idRegEx.Pattern = "^\d+$"
IsDigits = idRegEx.Test(str)
End Function


This function can be used like any other function in VBScript (i.e. IsNumeric()), but will not allow those other bad bad characters.

P.S. I told you guys I'd keep updating this, and I have been!:

http://www.solidscripts.com/downloads/functions.txt

;)

Mhtml
04-06-2003, 01:22 AM
Hmm... I was going to use regular expressions as I'm getting really good at them now (had to build smilies thingy for bb) . But I remembered about isNumeric(), I've never even used it lol...

Thanks for the info Whammy! :D

whammy
04-06-2003, 01:36 AM
You're welcome. :)

Just make sure when you validate for an expected result, that you test for other common entries - as in the example of IsNumeric(), to start with. ;)

Look at the sig, hehe. :D

Mhtml
04-06-2003, 01:38 AM
lol. :)

BigDaddy
04-06-2003, 07:44 AM
Hmm...

I've used isnumeric for an intranet site. Of course, the previous guy's version of form validation was the oracle error that was generated when the wrong data type was entered, so isnumeric is better than what was there before...

Whammy's got a point, though--in that the only way to TRULY validate for numeric only is to do the regex thing.

raf
04-06-2003, 09:46 AM
Good point whammy.

Gotta learn these regexs

But i always validate serversided (clientsided validation, please ...)+ set the db-fields to numeric. Don't think those values you said can be entered (i've tryed them, and it errored on me). But you never know what the RDBM makes of them (like +1,2 went to 1).

ooiooipig
04-07-2003, 01:51 AM
well guys, thanks for all the replies... however, what I've done till this point of time is as stated below... can i just add in the "regex" part that whammy suggested?? If so, where can I add it in??

action.asp
***************************************************

<%
if vReqtr = "" then
Response.write ("<br> Error: Please enter Requestor Name. <br><br>")%>
<input type=button value="Back" onClick="history.go(-1)">
<% Response.End()
end if
if vContactNo = "" then
Response.write ("<br> Error: Please enter Requestor Contact. <br><br>")%>
<input type=button value="Back" onClick="history.go(-1)" id=button1 name=button1>
<% Response.End()
end if
if vRC = "" then
Response.write ("<br> Error: Please enter RC. <br><br>")%>
<input type=button value="Back" onClick="history.go(-1)" id=button2 name=button2>
<% Response.End()
end if
if vAppnId = "" then
Response.write ("<br> Error: Please enter Name of Application. <br><br>")%>
<input type=button value="Back" onClick="history.go(-1)" id=button3 name=button3>
<% Response.End()
end if
if vreqTitle = "" then
Response.write ("<br> Error: Please enter Request Title. <br><br>")%>
<input type=button value="Back" onClick="history.go(-1)" id=button4 name=button4>
<% Response.End()
end if
if vfuncReq = "" then
Response.write ("<br> Error: Please enter Functional Requirement. <br><br>")%>
<input type=button value="Back" onClick="history.go(-1)" id=button5 name=button5>
<% Response.End()
end if
if vApprovOfficer = "" then
Response.write ("<br> Error: Please enter Approving Officer Name. <br><br>")%>
<input type=button value="Back" onClick="history.go(-1)" id=button6 name=button6>
<% Response.End()
end if
if vApprovEmail = "" then
Response.write ("<br> Error: Please enter Approving Officer Name Email. <br><br>")%>
<input type=button value="Back" onClick="history.go(-1)" id=button7 name=button7-->
<% Response.End()
end if
%>

ooiooipig
04-07-2003, 06:53 AM
hi anyone knows what error is this??

error line 12: " 'vContact.value' is null or not an object "
error line 68: " object expected "

I've written the javascript in the client side page and my codes are as follows:


<% reqType = Request.Form("reqType") %>

<html>
<head><title>client side</title>
<script language="JavaScript">
<!--

// function to validate form, checking error inputs by user
function VerifyData(f)
{
var vReqtr = f.vReqtr.value;
var vContact = f.vContact.value;
var vRC = f.vRC.value;
var vAppnId = f.vAppnId.value;
var vReqTitle = f.vReqTitle.value;
var vFuncReq = f.vFuncReq.value;
var vApprovOfficer = f.vApprovOfficer.value;
var vApprovEmail = f.vApprovEmail.value;
var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;

if(vReqtr == "")
alert("Key in Requestor Name");

else if(isNaN(vContact) == true || vContact.charAt(0)!= 6 && vContact.charAt(0)!= 9 )
alert("Key in Valid Contact");

else if(vRC== "")
alert("Key in RC");

else if(vAppnId== "")
alert("Key in Application Name");

else if(vReqTitle== "")
alert("Key in Request Title");

else if(vFuncReq== "")
alert("Key in Functional Requirement");

else if(vApprovOfficer== "")
alert("Key in Approving Officer Name");

else if(vApprovEmail== "")
alert("Key in Email");

else if (filter.test(vApprovEmail) != true)
alert("Please input a valid email address!");

else
{
var confirm2 = confirm ("Are you sure you wish to register?")
if (confirm2==true)
f.submit();
else
alert("please enter your correct particulars...")

}
}
-->
</script>
</head>

<body bgcolor="#FFCCCC">
<h2><u> Raise IS Request </u></h2>
<link rel="stylesheet" type="text/css" href="../../include/Stylesheet.css">
<table width="100%" border="0" cellspacing=0 align="center">
<tr>
<td>
<form name=form1 method="post" action="request_action.asp" onsubmit="return validate()">
<!--input type="hidden" name="dataAction" value="<%= request("dataaction")%>"-->

<% sql="ListReqTypeRef"
set rs = MSCS.Execute(sql)

if reqType = "" then
Response.write ("<br> Error: Please select the type of request. <br><br>")%>
<input type=button value="Back" onClick="history.go(-1)">
<% Response.End
rs.close
set rs=nothing
else %>
</td>
</tr>
<tr>
<td><input type="hidden" name="vreqType" value="<%=reqType%>" size="10" maxlength="10" readonly>&nbsp;</td>
</tr>
<tr>
<td><h3><u>Request By:</u></h3></td>
</tr>
<tr>
<td width="175"><span class="txtblack12">&nbsp;Name:&nbsp;</span></td>
<td><input type="text" name="vReqtr" value="" size="30" maxlength="50">&nbsp;</td>
<td width="150"><span class="txtblack12">&nbsp;Requestor Email:&nbsp;</span></td>
<td><input type=text name="vReqtrEmail" size="30" value="<%=Request.ServerVariables("LOGON_USER")%>@hotmail.com" readonly></td>
</tr>

<tr>
<td width="175"><span class="txtblack12">&nbsp;Contact No:&nbsp;</span></td>
<td><input type="text" name="vContactNo" value="" size="30" maxlength="10">&nbsp;</td>
<td width="150"><span class="txtblack12">&nbsp;Department:&nbsp;</span></td>
<td><input type="text" name="vDept" value="" size="30" maxlength="50">&nbsp;</td>
</tr>

<tr>
<td width="175"><span class="txtblack12">&nbsp;RC:&nbsp;</span></td>
<td width="205"><input type="text" name="vRC" value="" size="12" maxlength="10">&nbsp;</td>
<td width="150"><span class="txtblack12">&nbsp;Priority:&nbsp;</span></td>
<td><select name="vPriority">
<option>Low</option>
<option>Medium</option>
<option>High</option>
</select>
</td>
</tr>

<tr>
<td width="175"><span class="txtblack12">&nbsp;Name of Application System:&nbsp;</span></td>
<td width="205"><span class="txtblack12">
<%

'if reqType = "Application - Change Request" then
sql="select distinct ReqGrp from ReqTypeRef where reqType='" & ReqType & "'"
'sql="select AppnId from AppnRef where reqGrp='A'"
set rs = MSCS.Execute(sql)

if not rs.eof then%>

<select name="vAppnId">
<%do
sql="select distinct AppnId from AppnRef where reqGrp='" & rs("ReqGrp")& "'"
set rs1 = MSCS.Execute(sql)
if not rs1.eof then
sql="select count(distinct AppnId) as cnt from AppnRef where reqGrp='" & rs("ReqGrp")& "'"
set rsC = MSCS.Execute(sql)
if rsC("cnt") = 1 then
%>
<option value="<%=rs1("AppnId")%>" selected><%=rs1("AppnId")%></option>
<%
rsC.close
set rsC=nothing
else
if request("vAppnId") = "" then%>
<option value="" selected>Please select</option>
<%end if
do
%>
<option value="<%=rs1("AppnId")%>" <%if request("vAppnId") = rs1("AppnId") then%>selected<%end if%>><%=rs1("AppnId")%></option>
<%rs1.movenext
loop until rs1.eof
end if
end if
rs1.close
set rs1=nothing
rs.movenext
loop until rs.eof%>
</select>

<% end if

rs.close
set rs=nothing
'end if
%>
</span></td>
<td width="150"><span class="txtblack12">&nbsp;Desired Launched Date:&nbsp;</span></td>
<td><input type="text" name="vDesiredDt" value="" size="12" maxlength="10">
<input TYPE="button" NAME="DesiredDt" VALUE="Select Date" onClick="JavaScript:calendar = window.open('../include/calendar.asp?Form=form1&amp;DateControl=vDesiredDt','cal',' WIDTH=200,HEIGHT=230')"></td>
</tr>

<tr>
<table border="0">
<td width="170"><span class="txtblack12">&nbsp;Request Title:&nbsp;</span></td>
<td><input type="text" name="vreqTitle" value="" size="90" maxlength="100"></td>
</table>
</tr>

<tr>
<table border="0">
<td width="170"><span class="txtblack12">&nbsp;Functional Requirement:&nbsp;</span></td>
<td><textarea rows="5" cols="108" name="vfuncReq" maxlength="1000" class="txtblack12"></textarea></td>
</table>
</tr>



<% if reqType = "C" then %>

<tr>
<table border="0">
<td width="170"><span class="txtblack12">&nbsp;Frequency and Volume Estimate:&nbsp;</span></td>
<td><textarea rows="5" cols="108" name="vFreqVol" maxlength="1000" class="txtblack12"></textarea></td>
</table>
</tr>
<tr>
<td><h3><u>Expected Benefits (optional):</u></h3></td>
</tr>
<tr>
<table border="0">
<td width="170"><span class="txtblack12">&nbsp;Cost Savings($):&nbsp;</span></td>
<td width="205"><input type="text" name="vSavings" value="" size="12" maxlength="4">&nbsp;</td>
<td width="150"><span class="txtblack12">&nbsp;Revenue Earned($):&nbsp;</span></td>
<td width="205"><input type="text" name="vRevenue" value="" size="12" maxlength="4">&nbsp;</td>
</table>
</tr>
<tr>
<table border="0">
<td width="170"><span class="txtblack12">&nbsp;Tangible / Intangible Benefits:&nbsp;</span></td>
<td><textarea rows="5" cols="108" name="vBenefit" maxlength="1000" class="txtblack12"></textarea></td>
</table>
</tr>
<tr>
<td><h3><u>Tester (optional):</u></h3></td>
</tr>
<tr>
<table border="0">
<td width="170"><span class="txtblack12">&nbsp;Name:&nbsp;</span></td>
<td width="205"><input type="text" name="vTester" value="" size="30" maxlength="50">&nbsp;</td>
<td width="150"><span class="txtblack12">&nbsp;Contact:&nbsp;</span></td>
<td width="205"><input type="text" name="vTesterContact" value="" size="12" maxlength="10">&nbsp;</td>
</table>
</tr>

<% end if %>

<tr>
<td><h3><u>Approving Officer:</u></h3></td>
</tr>
<tr><table border="0">
<td width="170"><span class="txtblack12">&nbsp;Approving Officer Name:&nbsp;</span></td>
<td width="205"><input type="text" name="vApprovOfficer" value="" size="30" maxlength="50">&nbsp;</td>
<td width="150"><span class="txtblack12">&nbsp;Approving Officer Email:&nbsp;</span></td>
<td width="205"><input type="text" name="vApprovEmail" value="" size="30" maxlength="50">&nbsp;</td>
</table>
</tr>
<tr>
<P>&nbsp;</P>
<div align="center">
<input type="submit" name="insert" value="Submit" onClick="VerifyData(form1)">&nbsp;&nbsp;
<input type="reset" name="reset" value="Reset">&nbsp;&nbsp;
<input type="button" name="cancel" value="Cancel" onclick="javascript:window.history.back(-1)">
</div>
</tr>
<% end if %>
</form>

</table>
</body>
</html>

miranda
04-07-2003, 07:25 AM
re: error line 12: " 'vContact.value' is null or not an object "

where in your form do you have vContact ?? i found a vContactNo but did not find a vContact

so change the line that says vConact.value to be vContactNo.value

re: error line 68: " object expected "
points to onsubmit="return validate()"

but you do not have a function named validate instead you have one named VerifyData

ooiooipig
04-07-2003, 08:11 AM
hi miranda,
how come it will only prompt me that the Reqtr field is empty then when i click ok then it brings me to the confirmation page?? the other validation is not done... anyone who knows my problem please advise me what i can do. thanks!!

miranda
04-07-2003, 09:01 AM
What I do is use return (false); on each instance where it is bad like so

function VerifyData(f)
{
var vReqtr = f.vReqtr.value;
var vContact = f.vContact.value;
var vRC = f.vRC.value;
var vAppnId = f.vAppnId.value;
var vReqTitle = f.vReqTitle.value;
var vFuncReq = f.vFuncReq.value;
var vApprovOfficer = f.vApprovOfficer.value;
var vApprovEmail = f.vApprovEmail.value;
var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;


if(vReqtr == ""){
alert("Key in Requestor Name");
return (false);
}
else if(isNaN(vContact) == true || vContact.charAt(0)!= 6 && vContact.charAt(0)!= 9 ){
alert("Key in Valid Contact");
else if(vRC== ""){
alert("Key in RC");
return (false);
}
else if(vAppnId== ""){
alert("Key in Application Name");
return (false);
}

else if(vReqTitle== ""){
alert("Key in Request Title");
return (false);
}

else if(vFuncReq== ""){
alert("Key in Functional Requirement");
return (false);
}

else if(vApprovOfficer== ""){
alert("Key in Approving Officer Name");
return (false);
}

else if(vApprovEmail== ""){
alert("Key in Email");
return (false);
}

else if (filter.test(vApprovEmail) != true){
alert("Please input a valid email address!");
return (false);
}

else
{
var confirm2 = confirm ("Are you sure you wish to register?")
if (confirm2==true)
f.submit();
else
alert("please enter your correct particulars...")
return (false);
}
return (true);
}

ooiooipig
04-07-2003, 09:33 AM
hi miranda, I'm still having problem with validating the 2nd field... after it prompt the first message that the 1st field is not fielded in, then it goes to the success page... below is my present coding:

<html>
<head><title>client side</title>
<script language="JavaScript">
<!--

// function to validate form, checking error inputs by user
function VerifyData(f)
{
var vReqtr = f.vReqtr.value;
var vContactNo = f.vContactNo.value;
var vRC = f.vRC.value;
var vAppnId = f.vAppnId.value;
var vreqTitle = f.vreqTitle.value;
var vfuncReq = f.vfuncReq.value;
var vApprovOfficer = f.vApprovOfficer.value;
var vApprovEmail = f.vApprovEmail.value;
var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;

if(vReqtr == "")
{
alert("Please key in Requestor Name");
return (false);
}

else if(isNaN(vContactNo) == true || vContactNo.charAt(0)!= 6 && vContactNo.charAt(0)!= 9 )
{
alert("Please key in a Valid Contact");
return (false);
}

else if(vRC== "")
{
alert("Please key in RC");
return (false);
}

else if(vAppnId== "")
{
alert("Please key in Application Name");
return (false);
}

else if(vreqTitle== "")
{
alert("Please key in Request Title");
return (false);
}

else if(vfuncReq== "")
{
alert("Please key in Functional Requirement");
return (false);
}

else if(vApprovOfficer== "")
{
alert("Please key in Approving Officer Name");
return (false);
}

else if(vApprovEmail== "")
{
alert("Please key in Approving Officer's Email");
return (false);
}

else if (filter.test(vApprovEmail) != true)
{
alert("Please key in a valid Approving Officer's Email!");
return (false);
}
else
{
var confirm2 = confirm ("Are you sure you wish to submit the request?")
if (confirm2==true)
f.submit();
else
{
alert("please enter your correct particulars...");
return (false);
}
}
}
-->
</script>
</head>

<body bgcolor="#FFFFCC">
<h2><u> Raise IS Request </u></h2>
<link rel="stylesheet" type="text/css" href="../include/Stylesheet.css">
<table width="100%" border="0" cellspacing=0 align="center">
<tr>
<td>
<form name=form1 method="post" action="request_action.asp">
<!--input type="hidden" name="dataAction" value="<%= request("dataaction")%>"-->

<% sql="ListReqTypeRef"
set rs = MSCS.Execute(sql)

if reqType = "" then
Response.write ("<br> Error: Please select the type of request. <br><br>")%>
<input type=button value="Back" onClick="history.go(-1)">
<% Response.End
rs.close
set rs=nothing
else %>
</td>
</tr>
<tr>
<td><input type="hidden" name="vreqType" value="<%=reqType%>" size="10" maxlength="10" readonly>&nbsp;</td>
</tr>
<tr>
<td><h3><u>Request By:</u></h3></td>
</tr>
<tr>
<td width="175"><span class="txtblack12">&nbsp;Name:&nbsp;</span></td>
<td><input type="text" name="vReqtr" value="" size="30" maxlength="50">&nbsp;</td>
<td width="150"><span class="txtblack12">&nbsp;Requestor Email:&nbsp;</span></td>
<td><input type=text name="vReqtrEmail" size="30" value="<%=Request.ServerVariables("LOGON_USER")%>@hotmail.com" readonly></td>
</tr>

<tr>
<td width="175"><span class="txtblack12">&nbsp;Contact No:&nbsp;</span></td>
<td><input type="text" name="vContactNo" value="" size="30" maxlength="10">&nbsp;</td>
<td width="150"><span class="txtblack12">&nbsp;Department:&nbsp;</span></td>
<td><input type="text" name="vDept" value="" size="30" maxlength="50">&nbsp;</td>
</tr>

<tr>
<td width="175"><span class="txtblack12">&nbsp;RC:&nbsp;</span></td>
<td width="205"><input type="text" name="vRC" value="" size="12" maxlength="10">&nbsp;</td>
<td width="150"><span class="txtblack12">&nbsp;Priority:&nbsp;</span></td>
<td><select name="vPriority">
<option>Low</option>
<option>Medium</option>
<option>High</option>
</select>
</td>
</tr>

<tr>
<td width="175"><span class="txtblack12">&nbsp;Name of Application System:&nbsp;</span></td>
<td width="205"><span class="txtblack12">
<%

'if reqType = "Application - Change Request" then
sql="select distinct ReqGrp from ReqTypeRef where reqType='" & ReqType & "'"
'sql="select AppnId from AppnRef where reqGrp='A'"
set rs = MSCS.Execute(sql)

if not rs.eof then%>

<select name="vAppnId">
<%do
sql="select distinct AppnId from AppnRef where reqGrp='" & rs("ReqGrp")& "'"
set rs1 = MSCS.Execute(sql)
if not rs1.eof then
sql="select count(distinct AppnId) as cnt from AppnRef where reqGrp='" & rs("ReqGrp")& "'"
set rsC = MSCS.Execute(sql)
if rsC("cnt") = 1 then
%>
<option value="<%=rs1("AppnId")%>" selected><%=rs1("AppnId")%></option>
<%
rsC.close
set rsC=nothing
else
if request("vAppnId") = "" then%>
<option value="" selected>Please select</option>
<%end if
do
%>
<option value="<%=rs1("AppnId")%>" <%if request("vAppnId") = rs1("AppnId") then%>selected<%end if%>><%=rs1("AppnId")%></option>
<%rs1.movenext
loop until rs1.eof
end if
end if
rs1.close
set rs1=nothing
rs.movenext
loop until rs.eof%>
</select>

<% end if

rs.close
set rs=nothing
'end if
%>
</span></td>
<td width="150"><span class="txtblack12">&nbsp;Desired Launched Date:&nbsp;</span></td>
<td><input type="text" name="vDesiredDt" value="" size="12" maxlength="10">
<input TYPE="button" NAME="DesiredDt" VALUE="Select Date" onClick="JavaScript:calendar = window.open('../include/calendar.asp?Form=form1&amp;DateControl=vDesiredDt','cal',' WIDTH=200,HEIGHT=230')"></td>
</tr>

<tr>
<table border="0">
<td width="170"><span class="txtblack12">&nbsp;Request Title:&nbsp;</span></td>
<td><input type="text" name="vreqTitle" value="" size="90" maxlength="100"></td>
</table>
</tr>

<tr>
<table border="0">
<td width="170"><span class="txtblack12">&nbsp;Functional Requirement:&nbsp;</span></td>
<td><textarea rows="5" cols="108" name="vfuncReq" maxlength="1000" class="txtblack12"></textarea></td>
</table>
</tr>



<% if reqType = "C" then %>

<tr>
<table border="0">
<td width="170"><span class="txtblack12">&nbsp;Frequency and Volume Estimate:&nbsp;</span></td>
<td><textarea rows="5" cols="108" name="vFreqVol" maxlength="1000" class="txtblack12"></textarea></td>
</table>
</tr>
<tr>
<td><h3><u>Expected Benefits (optional):</u></h3></td>
</tr>
<tr>
<table border="0">
<td width="170"><span class="txtblack12">&nbsp;Cost Savings($):&nbsp;</span></td>
<td width="205"><input type="text" name="vSavings" value="" size="12" maxlength="4">&nbsp;</td>
<td width="150"><span class="txtblack12">&nbsp;Revenue Earned($):&nbsp;</span></td>
<td width="205"><input type="text" name="vRevenue" value="" size="12" maxlength="4">&nbsp;</td>
</table>
</tr>
<tr>
<table border="0">
<td width="170"><span class="txtblack12">&nbsp;Tangible / Intangible Benefits:&nbsp;</span></td>
<td><textarea rows="5" cols="108" name="vBenefit" maxlength="1000" class="txtblack12"></textarea></td>
</table>
</tr>
<tr>
<td><h3><u>Tester (optional):</u></h3></td>
</tr>
<tr>
<table border="0">
<td width="170"><span class="txtblack12">&nbsp;Name:&nbsp;</span></td>
<td width="205"><input type="text" name="vTester" value="" size="30" maxlength="50">&nbsp;</td>
<td width="150"><span class="txtblack12">&nbsp;Contact:&nbsp;</span></td>
<td width="205"><input type="text" name="vTesterContact" value="" size="12" maxlength="10">&nbsp;</td>
</table>
</tr>

<% end if %>

<tr>
<td><h3><u>Approving Officer:</u></h3></td>
</tr>
<tr><table border="0">
<td width="170"><span class="txtblack12">&nbsp;Approving Officer Name:&nbsp;</span></td>
<td width="205"><input type="text" name="vApprovOfficer" value="" size="30" maxlength="50">&nbsp;</td>
<td width="150"><span class="txtblack12">&nbsp;Approving Officer Email:&nbsp;</span></td>
<td width="205"><input type="text" name="vApprovEmail" value="" size="30" maxlength="50">&nbsp;</td>
</table>
</tr>
<tr>
<P>&nbsp;</P>
<div align="center">
<input type="submit" name="insert" value="Submit" onClick="VerifyData(form1)">&nbsp;&nbsp;
<input type="reset" name="reset" value="Reset">&nbsp;&nbsp;
<input type="button" name="cancel" value="Cancel" onclick="javascript:window.history.back(-1)">
</div>
</tr>
<% end if %>
</form>

</table>
</body>
</html>

miranda
04-07-2003, 03:44 PM
I loaded your page in Visual Studio and found these errors

#1
<input TYPE="button" NAME="DesiredDt" VALUE="Select Date" onClick="java script:calendar = window.open('../include/calendar.asp?Form=form1&DateControl=vDesiredDt','cal',' WIDTH=200,HEIGHT=230')"></td>

should be
<input TYPE="button" NAME="DesiredDt" VALUE="Select Date" onClick="javascript:calendar = window.open('../include/calendar.asp?Form=form1&DateControl=vDesiredDt','cal',' WIDTH=200,HEIGHT=230')"></td>

Do Not add a space in Javascript

#2
<input type="button" name="cancel" value="Cancel" onclick="java script:window.history.back(-1)">

should be
<input type="button" name="cancel" value="Cancel" onclick="javascript:history.go(-1)">

again space added in javascript also correct format to go back one page is history.go(-1) you can put the window in there but it is not needed

#3
<input type="submit" name="insert" value="Submit" onClick="VerifyData(form1)">

should be
<input type="submit" name="insert" value="Submit" onClick="return VerifyData(form1)">


You forgot to include the return in the onClick event handler. This is why it was not validating all fields.

To make your form more user friendly you may want to put focus on the missing form field after the alert like so:

if(vReqtr == "")
{
alert("Please key in Requestor Name");
f.vReqtr.focus();
return (false);
}

if(isNaN(vContactNo) == true || vContactNo.charAt(0)!= 6 && vContactNo.charAt(0)!= 9 )
{
alert("Please key in a Valid Contact");
f.vContactNo.select();
f.vContactNo.focus();
return (false);
}


now notice that I took out the else on the second field to validate? It isn't needed, nor is it needed on the others. You can leave it in or not, that is up to you. Also on that one I added one more line
f.vContactNo.select();
what this does is highlights the number if a bad number is entered. for example if it doesn't start with a 6 or a 9

whammy
04-07-2003, 11:41 PM
Big Daddy knows of which sites I'm speaking of, 'cause he used to work with me. So he can vouch for at least some of the validation problems (he didn't write that particular part either).

Which reminds me, for standards I'm going to suggest certain validation practices in this case, since at work we usually use the same vendor. :)

ooiooipig
04-08-2003, 02:26 AM
Hi all, how about if i have the optional field (the vTesterContact in my code above) , how do i validate it to make sure that the user has entered a valid number?? the field may not necessary be field in... however, if the user wants to key in the number, he or she must key in a numerical value and not alphabets... can anyone help me??

whammy
04-08-2003, 02:36 AM
I already posted a function for that to validate server-side; all you have to do client-side is use the same regular expression client-side, i.e.:

if(!/^\d+$/.test(num) {
alert('NOT ALL DIGITS!!!');
}

whammy
04-08-2003, 02:37 AM
P.S. If it's not required:

if(num.length > 0 && !/^\d+$/.test(num) {
alert('NOT ALL DIGITS!!!');
}


__________________

ooiooipig
04-08-2003, 03:19 AM
how about if i wan the user to begin their entered value with either a 6 or 9?? how do i write the code?? this is what i've done but it doesnt seem to work for the begin with 6 or 9 part. can anyone help me check which part I've done wrongly?? thanks!

else if (vTesterContact.length > 0 && !/^\d+$/.test(vTesterContact) && vTesterContact.charAt(0)!= 6 && vTesterContact.charAt(0)!= 9)
{
alert("Please enter a valid number for Tester Contact");
f.vTesterContact.focus();
return (false);
}

Morgoth
04-08-2003, 04:23 AM
I suggest people should use the [ code ] tags...

Prevents viewing issues...

ooiooipig
04-08-2003, 08:05 AM
Hi all, now i have another problem. Under some topics where some fields are not required to field in, the field will not be shown out. However, under this topics, the validations mentioned above cannot work! :confused:

but under the topic where all the fields are needed to field in, the validation can work. can anyone advise me what to do??

below are my codings:


<!--#include file="../include/dbconn.asp"-->

<html>
<head><title>client side</title>
<script language="JavaScript">
<!--

// function to validate form, checking error inputs by user
function VerifyData(f)
{
var vReqtr = f.vReqtr.value;
var vContactNo = f.vContactNo.value;
var vRC = f.vRC.value;
var vAppnId = f.vAppnId.value;
var vreqTitle = f.vreqTitle.value;
var vfuncReq = f.vfuncReq.value;
var vSavings = f.vSavings.value;
var vRevenue = f.vRevenue.value;
var vTesterContact = f.vTesterContact.value;
var vApprovOfficer = f.vApprovOfficer.value;
var vApprovEmail = f.vApprovEmail.value;
var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;

if(vReqtr == "")
{
alert("Please key in Requestor Name");
f.vReqtr.focus();
return (false);
}

else if(isNaN(vContactNo) == true || vContactNo.charAt(0)!= 6 && vContactNo.charAt(0)!= 9 )
{
alert("Please key in a Valid Contact that begin with a 6 or 9");
f.vContactNo.select();
f.vContactNo.focus();
return (false);
}

else if(vRC == "")
{
alert("Please key in RC");
f.vRC.focus();
return (false);
}

else if(vAppnId == "")
{
alert("Please select an Application Name");
f.vAppnId.focus();
return (false);
}

else if(vreqTitle == "")
{
alert("Please key in Request Title");
f.vreqTitle.focus();
return (false);
}

else if(vfuncReq == "")
{
alert("Please key in Functional Requirement");
f.vfuncReq.focus();
return (false);
}

else if(isNaN(vSavings) == true )
{
alert("Please key in a numerical value for Cost Savings");
f.vSavings.select();
f.vSavings.focus();
return (false);
}

else if(isNaN(vRevenue) == true )
{
alert("Please key in a numerical value for Revenue");
f.vRevenue.select();
f.vRevenue.focus();
return (false);
}

else if(vApprovOfficer == "")
{
alert("Please key in Approving Officer Name");
f.vApprovOfficer.focus();
return (false);
}

else if(vApprovEmail == "")
{
alert("Please key in Approving Officer's Email");
f.vApprovEmail.focus();
return (false);
}

else if (filter.test(vApprovEmail) != true)
{
alert("Please key in a valid Approving Officer's Email!");
f.vApprovEmail.focus();
return (false);
}

else if (vTesterContact.length > 0 && (!/^\d+$/.test(vTesterContact) || vTesterContact.charAt(0)!= 6 && vTesterContact.charAt(0)!= 9))
{
alert("Please enter a valid number for Tester Contact that begin with a 6 or 9");
f.vTesterContact.focus();
return (false);
}

else
{
var confirm2 = confirm ("Are you sure you wish to submit the request?")
if (confirm2==true)
f.submit();
else
{
return (false);
}
}
}
-->
</script>
</head>

<body bgcolor="#FFFFCC">
<h2><u> client side</u></h2>
<link rel="stylesheet" type="text/css" href="../include/Stylesheet.css">
<table width="100%" border="0" cellspacing=0 align="center">
<tr>
<td>
<form name=form1 method="post" action="request_action.asp">
<!--input type="hidden" name="dataAction" value="<%= request("dataaction")%>"-->

<% sql="ListReqTypeRef"
set rs = MSCS.Execute(sql)

if reqType = "" then
Response.write ("<br> Error: Please select the type of request. <br><br>")%>
<input type=button value="Back" onClick="history.go(-1)">
<% Response.End
rs.close
set rs=nothing
else %>
</td>
</tr>
<tr>
<td><input type="hidden" name="vreqType" value="<%=reqType%>" size="10" maxlength="10" readonly>&nbsp;</td>
</tr>
<tr>
<td><h3><u>Request By:</u></h3></td>
</tr>
<tr>
<td width="175"><span class="txtblack12">&nbsp;* Name:&nbsp;</span></td>
<td><input type="text" name="vReqtr" value="" size="30" maxlength="50">&nbsp;</td>
<td width="150"><span class="txtblack12">&nbsp;Requestor Email:&nbsp;</span></td>
<td><input type=text name="vReqtrEmail" size="30" value="<%=Request.ServerVariables("LOGON_USER")%>@hotmail.com" readonly></td>
</tr>

<tr>
<td width="175"><span class="txtblack12">&nbsp;* Contact No:&nbsp;</span></td>
<td><input type="text" name="vContactNo" value="" size="30" maxlength="8">&nbsp;</td>
<td width="150"><span class="txtblack12">&nbsp;Department:&nbsp;</span></td>
<td><input type="text" name="vDept" value="" size="30" maxlength="50">&nbsp;</td>
</tr>

<tr>
<td width="175"><span class="txtblack12">&nbsp;* RC:&nbsp;</span></td>
<td width="205"><input type="text" name="vRC" value="" size="12" maxlength="10">&nbsp;</td>
<td width="150"><span class="txtblack12">&nbsp;Priority:&nbsp;</span></td>
<td><select name="vPriority">
<option>Low</option>
<option>Medium</option>
<option>High</option>
</select>
</td>
</tr>

<tr>
<td width="175"><span class="txtblack12">&nbsp;* Name of Application System:&nbsp;</span></td>
<td width="205"><span class="txtblack12">
<%

'if reqType = "Application - Change Request" then
sql="select distinct ReqGrp from ReqTypeRef where reqType='" & ReqType & "'"
'sql="select AppnId from AppnRef where reqGrp='A'"
set rs = MSCS.Execute(sql)

if not rs.eof then%>

<select name="vAppnId">
<%do
sql="select distinct AppnId from AppnRef where reqGrp='" & rs("ReqGrp")& "'"
set rs1 = MSCS.Execute(sql)
if not rs1.eof then
sql="select count(distinct AppnId) as cnt from AppnRef where reqGrp='" & rs("ReqGrp")& "'"
set rsC = MSCS.Execute(sql)
if rsC("cnt") = 1 then
%>
<option value="<%=rs1("AppnId")%>" selected><%=rs1("AppnId")%></option>
<%
rsC.close
set rsC=nothing
else
if request("vAppnId") = "" then%>
<option value="" selected>Please select</option>
<%end if
do
%>
<option value="<%=rs1("AppnId")%>" <%if request("vAppnId") = rs1("AppnId") then%>selected<%end if%>><%=rs1("AppnId")%></option>
<%rs1.movenext
loop until rs1.eof
end if
end if
rs1.close
set rs1=nothing
rs.movenext
loop until rs.eof%>
</select>

<% end if

rs.close
set rs=nothing
'end if
%>
</span></td>
<td width="150"><span class="txtblack12">&nbsp;Desired Launched Date:&nbsp;</span></td>
<td><input type="text" name="vDesiredDt" value="" size="12" maxlength="10">
<input TYPE="button" NAME="DesiredDt" VALUE="Select Date" onClick="Javascript:calendar = window.open('../include/calendar.asp?Form=form1&amp;DateControl=vDesiredDt','cal',' WIDTH=200,HEIGHT=230')"></td>
</tr>

<tr>
<table border="0">
<td width="170"><span class="txtblack12">&nbsp;* Request Title:&nbsp;</span></td>
<td><input type="text" name="vreqTitle" value="" size="90" maxlength="100"></td>
</table>
</tr>

<tr>
<table border="0">
<td width="170"><span class="txtblack12">&nbsp;* Functional Requirement:&nbsp;</span></td>
<td><textarea rows="5" cols="108" name="vfuncReq" maxlength="1000" class="txtblack12"></textarea></td>
</table>
</tr>



<% if reqType = "C" then %>

<tr>
<table border="0">
<td width="170"><span class="txtblack12">&nbsp;Frequency and Volume Estimate:&nbsp;</span></td>
<td><textarea rows="5" cols="108" name="vFreqVol" maxlength="800" class="txtblack12"></textarea></td>
</table>
</tr>
<tr>
<td><h3><u>Expected Benefits (optional):</u></h3></td>
</tr>
<tr>
<table border="0">
<td width="170"><span class="txtblack12">&nbsp;Cost Savings($):&nbsp;</span></td>
<td width="205"><input type="text" name="vSavings" value="" size="12" maxlength="4">&nbsp;</td>
<td width="150"><span class="txtblack12">&nbsp;Revenue Earned($):&nbsp;</span></td>
<td width="205"><input type="text" name="vRevenue" value="" size="12" maxlength="4">&nbsp;</td>
</table>
</tr>
<tr>
<table border="0">
<td width="170"><span class="txtblack12">&nbsp;Tangible / Intangible Benefits:&nbsp;</span></td>
<td><textarea rows="5" cols="108" name="vBenefit" maxlength="800" class="txtblack12"></textarea></td>
</table>
</tr>
<tr>
<td><h3><u>Tester (optional):</u></h3></td>
</tr>
<tr>
<table border="0">
<td width="170"><span class="txtblack12">&nbsp;Name:&nbsp;</span></td>
<td width="205"><input type="text" name="vTester" value="" size="30" maxlength="50">&nbsp;</td>
<td width="150"><span class="txtblack12">&nbsp;Contact:&nbsp;</span></td>
<td width="205"><input type="text" name="vTesterContact" value="" size="12" maxlength="8">&nbsp;</td>
</table>
</tr>

<% end if %>

<tr>
<td><h3><u>Approving Officer:</u></h3></td>
</tr>
<tr><table border="0">
<td width="170"><span class="txtblack12">&nbsp;* Approving Officer Name:&nbsp;</span></td>
<td width="205"><input type="text" name="vApprovOfficer" value="" size="30" maxlength="50">&nbsp;</td>
<td width="150"><span class="txtblack12">&nbsp;* Approving Officer Email:&nbsp;</span></td>
<td width="205"><input type="text" name="vApprovEmail" value="" size="30" maxlength="50">&nbsp;</td>
</table>
</tr>
<tr>
<P>&nbsp;</P>
<div align="center">
<input type="submit" name="insert" value="Submit" onClick="return VerifyData(form1)">&nbsp;&nbsp;
<input type="reset" name="reset" value="Reset">&nbsp;&nbsp;
<input type="button" name="cancel" value="Cancel" onclick="javascript:window.history.back(-1)">
</div>
</tr>
<% end if %>
</form>

</table>
</body>
</html>
<!--#include file="../include/dbclose.asp"-->