View Full Version : Validation code

02-12-2004, 09:51 PM
I have a piece of code that uses an image to provide validation alerts for the user. I want this code to validate that the user is entering information and that they are only entering numbers. It works when the length of the field is fixed (i.e. 11 digits), however whenever the length of the field is variable (i.e. 5-10 digits) it just displays the alert image.

var empty = new Image(); empty.src = "images/fieldempty.gif";

var haveerrors = 0;
function showImage(imagename, imageurl, errors) {
document[imagename].src = imageurl;
if (!haveerrors && errors) haveerrors = errors;

function validateForm() {
var valid = "0123456789"
haveerrors = 0;
(document.ship_num.txtShipNum.value =="" || document.ship_num.txtShipNum.value.search(valid) == -1) // validate first name length
? showImage("itemerror", "images/shippingnumber.gif", true) // no semi-colon after this line!
: showImage("itemerror", "images/blankimage.gif", false); // true = errors, false = no errors

return (!haveerrors);

<form action="ship_num_lookup_results.asp" method="get" name="ship_num" target="bottomFrame" id="ship_num"
onSubmit="return validateForm(this)">