Click to See Complete Forum and Search --> : Validate Fields??


ekta
01-26-2004, 03:17 PM
I have an asp page that has the field LOG ID NUMBER. Now this field takes in 2 parameters. One is LOGID_YY(2 character long) and the other is LOGID_NO(8 characters long). So it looks like this
LOG ID NUMBER: __ ________

User enters criteria here to run reports. I want the page to display an error if user enters just the year and not the logid_no. Vice-versa is not true. They can enter a logid_no w/o the year.

I am attaching both the asp and js files.

I would really appreciate if any one can help me with this.

Thanks

Ekta

olerag
01-26-2004, 06:29 PM
Its much easier (and probably more comprehensible) if you
split these into two separate fields however, if you insist on
one field sharing two separate data elements, utilize both
the String.length and String.substring functions to validate
your requirements.

ekta
01-27-2004, 07:37 AM
Thanx for replying Olerag. Actually this system was developed by somebody else and that's how they have done it. I am not very good with programming. Can you please explain a bit more on how to use String.length and String.substring functions to validate this field.

Thanx

olerag
01-27-2004, 09:45 AM
Sorry - snow day (late again).
This sample provides several examples of the String functions
previously mentioned. In this example, only 8 or 10 string
entries are permitted and, if the entry is 10 chars in length,
the first two MUST BE numbers.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Sample</title>
<script type="text/javascript">
<!--
function validate(str) {
var success = true;
if (str.length == 10 || str.length == 8) {
if (str.length == 8) {
alert("Report only: " + str);
}
else {
if (!isNumber(str.substring(0,2))) {
success = false;
}
else {
alert("Year and report:\n" + "Year: " + str.substring(0,2) + "\n" + "Report: " + str.substring(2,str.length));
}
}
}
else {
success = false;
}

if (!success) {
alert("Validation failed: " + str);
document.forms[0].field1.focus();
}
}

function isNumber(str) {
var char;
for (var i=0; i<str.length; i++) {
char = str.charAt(i).charCodeAt(0);
if(char < 48 || char > 57) {
return(false);
}
}
return(true);
}
// -->
</script>

<div style="text-align: center;">
<strong>Sample</strong>
</div>

<form action="">
<p>
<input type="text" name="field1" length="10" size="15">
</p>
<p style="text-align: center;">
<input type="button" name="btn1" value="Execute" onClick="validate(this.form.field1.value)">
</p>
</form>

ekta
01-27-2004, 10:13 AM
Yea, snow is bad. Today its even worse coz of black ice everywhere.

I looked at the code and kinda understand it. Can you please help me implement this in my code? I can't figure out how I should do this.

olerag
01-27-2004, 12:38 PM
I viewed your asp file (rep.asp) and cannot find any text
field that meets the criteria specified in your first thread; i.e.,
one field (having a maxlength of 10) used to stored both
the year and report number.

Before there is any continuance, you'll need to tell me the
object (specified in either the "name" or "id" parameter)
you are referring to.

Or, if you already know what this object is, assign a onBlur()
event to this item and call the modified JS script below.
You can alter your asp file as needed and add the JS
script to your existing "rep.js" file.

<input type="text" name="objName" onBlur="return validateYYReportField(this.value);">

<script type="text/javascript">
<!--
function validateYYReportField(str) {
if (str.length == 10 || str.length == 8) {
if (str.length == 8) {
return(true);
}
else {
if (!isNumber(str.substring(0,2))) {
success = false;
}
else {
return(true);
}
}
}
else {
success = false;
}

if (!success) {
alert("Validation failed: " + str);
return(false);
}
}

function isNumber(str) {
var char;
for (var i=0; i<str.length; i++) {
char = str.charAt(i).charCodeAt(0);
if(char < 48 || char > 57) {
return(false);
}
}
return(true);
}
// -->
</script>

ekta
01-27-2004, 01:49 PM
On the asp page, search for LOG ID NUMBER. It is made up of 2 parts. LOGID_YY(2 characters in length) and LOGID_NO(8 characters in length).
rep.js includes the code for validation of report parameters.