Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

validation - alpha and numeric - how to modify this script?

Status
Not open for further replies.

leeolive

Programmer
May 14, 2002
46
0
0
GB
Hi

I have found script which doesn't use alerts (from Javascript Source). It checks for empty, and min and max no of characters and email. I am trying to add functionality to check for alpha characters and then also for numeric. Could someone please help me with this as I am new to JavaScript? I think it's the syntax.. I got the script from here
I guess it would be something like this for alpha
(f.MaidenName.value !=("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-'") ???

I have this so far

<html>
<head><title></title>

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!-- Original: Jeff Harding (jbh@site-ations.com) -->
<!-- Web Site: -->
<!-- Modified by: Ronnie T. Moore, Editor -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! -->

<!-- Begin
// Preload images
var empty = new Image(); empty.src = &quot;images/fieldempty.gif&quot;;
var email = new Image(); email.src = &quot;images/emailerror.gif&quot;;
var zipcd = new Image(); zipcd.src = &quot;images/ziperror.gif&quot;;
var phone = new Image(); phone.src = &quot;images/phoneerror.gif&quot;;

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

function validateForm(f) {
haveerrors = 0;
(f.fname.value.length < 1) // validate first name length
? showImage(&quot;firstnameerror&quot;, &quot;fieldempty.gif&quot;, true) // no semi-colon after this line!
: showImage(&quot;firstnameerror&quot;, &quot;blankimage.gif&quot;, false); // true = errors, false = no errors

(f.lname.value.length < 1) // validate last name length
? showImage(&quot;lastnameerror&quot;, &quot;fieldempty.gif&quot;, true)
: showImage(&quot;lastnameerror&quot;, &quot;blankimage.gif&quot;, false);

(f.zip.value.length != 5) // validate zip code length
? showImage(&quot;ziperror&quot;, &quot;ziperror.gif&quot;, true)
: showImage(&quot;ziperror&quot;, &quot;blankimage.gif&quot;, false);

phonenumlength = f.area.value.length +
f.exchange.value.length + f.number.value.length;

(phonenumlength != 10) // validate phone number length
? showImage(&quot;phoneerror&quot;, &quot;phoneerror.gif&quot;, true)
: showImage(&quot;phoneerror&quot;, &quot;blankimage.gif&quot;, false);

(f.email.value.search(&quot;@&quot;) == -1 || f.email.value.search(&quot;[.*]&quot;) == -1) // validate email
? showImage(&quot;emailerror&quot;, &quot;emailerror.gif&quot;, true)
: showImage(&quot;emailerror&quot;, &quot;blankimage.gif&quot;, false);

return (!haveerrors);
}
// End -->
</script>

</head>
<body>

<center>
<form action=&quot;script.cgi&quot; name=&quot;myform&quot; onSubmit=&quot;return validateForm(this)&quot;>
<table border=0 cellspacing=0 celpadding=0>
<tr>
<td colspan=2>Enter your information:<br>
<sup>(<font color=&quot;#ff0000&quot;>*</font> denotes required field).</sup></td>
</tr>
<tr>
<td><p align=right>
First Name:</td>
<td>
<input type=text name=&quot;fname&quot; size=25 maxlength=50><font color=&quot;#ff0000&quot;>*</font><br>
<img name=firstnameerror src=&quot;blankimage.gif&quot; width=350 height=10 border=0></td>
</tr>
<tr>
<td><p align=right>
Last Name:</td>
<td>
<input type=text name=&quot;lname&quot; size=25 maxlength=50><font color=&quot;#ff0000&quot;>*</font><br>
<img name=lastnameerror src=&quot;blankimage.gif&quot; width=350 height=10 border=0></td>
</tr>
<tr>
<td><p align=right>
Zip Code:</td>
<td>
<input type=text name=zip size=25 maxlength=50><font color=&quot;#ff0000&quot;>*</font><br>
<img name=ziperror src=&quot;blankimage.gif&quot; width=350 height=10 border=0></td>
</tr>
<tr>
<td><p align=right>
Email:</td>
<td>
<input type=text name=email size=25 maxlength=50><font color=&quot;#ff0000&quot;>*</font><br>
<img name=emailerror src=&quot;blankimage.gif&quot; width=350 height=10 border=0></td>
</tr>
<tr>
<td><p align=right>
Phone:</td>
<td>
<input type=text name=area size=4 maxlength=5>-
<input type=text name=exchange size=4 maxlength=3>-
<input type=text name=number size=5 maxlength=4><font color=&quot;#ff0000&quot;>*</font><br>
<img name=phoneerror src=&quot;blankimage.gif&quot; width=350 height=10 border=0></td>
</tr>
<tr>
<td colspan=2><p align=center>
<hr>
</td>
</tr>
<tr>
<td><p align=center>
</td>
<td><p align=right>
<input type=submit value=&quot;Submit Form&quot;></td>
</tr>
</table>
</form>
</center>

<p><center>
<font face=&quot;arial, helvetica&quot; size=&quot;-2&quot;>Free JavaScripts provided<br>
by <a href=&quot; JavaScript Source</a></font>
</center><p>

</body>
</html>


Thanks for your help!
Lee
 
er, this will do:
var str=&quot;ASDASDASDASD&quot;
if(!str.match(/^[A-Za-z0-9_]+$/))
{
alert(&quot;Str has values other than alpha numeric and numbers and _.&quot;)
}

does this help?

Known is handfull, Unknown is worldfull
 
Thanks for replying! I need to check only for alpha characters (once I have figured out alpha, numeric should be easy). I am trying to get it to become part of the script that I already have as it works well. The alpha checking line needs to fit this format/syntax

(f.email.value.search(&quot;@&quot;) == -1 || f.email.value.search(&quot;[.*]&quot;) == -1)

or this format
(f.fname.value.length < 1)

I have this, but I don't think I have the logic right.
(f.MaidenName.value.search(&quot;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-'&quot;) == -1)

Basically what I want to say is &quot;if value entered in the text box does not equal any of these characters abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-'&quot;

then (the rest as seen below is easy - I can do)
? showImage(&quot;firstnameerror&quot;, &quot;images/fieldempty.gif&quot;, true) // no semi-colon after this line!
: showImage(&quot;firstnameerror&quot;, &quot;images/blankimage.gif&quot;, false); // true

(Depending on the argument - if the outcome is true then it will call on an image which says 'not alpha'.)

Thanks!
Lee
 
Using vbkris' idea to include regular expressions and
make things alot easier try this:

var realph = /^[a-zA-Z-]+$/;
var renums = /^[0-9-]+$/;

(!f.lname.value.match(realph)) // validate last name
? showImage(&quot;lastnameerror&quot;, &quot;fieldempty.gif&quot;, true)
: showImage(&quot;lastnameerror&quot;, &quot;blankimage.gif&quot;, false);

modified for other field:

(!f.MaidenName.value.match(realph)) //valid maiden name
? showImage(&quot;lastnameerror&quot;, &quot;fieldempty.gif&quot;, true)
: showImage(&quot;lastnameerror&quot;, &quot;blankimage.gif&quot;, false);

And you can use the renums for the phone if you want.

You might want to add an extra image name and gif for the extra field.

Try this working example for an idea how it works:
Code:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>
<html><head><title>TEST</title>
<script>
function ckalph(str) {
 var re = /^[a-zA-Z-]+$/;
 if(!str.match(re)) { alert(&quot;Bad Char&quot;); }
 else { alert(&quot;Good String&quot;); }
}
function cknum(str) {
 var re = /^[0-9-]+$/;
 if(!str.match(re)) { alert(&quot;Bad Char&quot;); }
 else { alert(&quot;Good String&quot;); }
}
</script></head><body>Enter Alph:
<input type=&quot;text&quot; name=&quot;tst&quot; onBlur=&quot;ckalph(this.value);&quot;>
<br>Enter Numbers:
<input type=&quot;text&quot; name=&quot;tstb&quot; onBlur=&quot;cknum(this.value);&quot;>
</body></html>

2b||!2b
 
Thanks for getting back!

It seems to be working :)

only thing is that I need to also allow hyphens and apostrophe's in the alpha set as some people may have those in their names. I tried to add it within the [] and outside but it doesn't take any notice - still finds it false.

I also need to allow for commas and points(,.) in the numeric variable.

function validateForm(f) {
var realph = /^[a-zA-Z-]+$/;
var renums = /^[0-9-]+$/;

Any idea?
Thanks!
Lee
 
Try:

var realph = /^[a-zA-Z\'-]+$/;
var renums = /^[0-9\.\,-]+$/;

Let us know if any trouble....

2b||!2b
 
You're a star!

One more question..I have over forty fields I need to validate and the script is getting so long. I'd like to place it in an external js file, like so
<script language=&quot;JavaScript&quot; src=&quot;formvalidation.js&quot;></script>

but how do I call on it if my form tag is as follows:
<form action=&quot;submit.asp&quot; name=&quot;myform&quot; onSubmit=&quot;return validateForm(this)&quot;>

thanks for you help!
Lee
 
When you supply an external src there is no difference
in calling the functions. It's just like when it is on
the page.

This is how you load from src:

<script src=&quot;ursource.js&quot; type=&quot;text/javascript&quot;></script>

Preferably in the <head> section of the page.

Make sure you reference a relative path to the
source.


2b||!2b
 
Thanks, must I leave out the <script> tags within the external js file? Sorry but I can't test it until I have completed the entire thing as I've done half.
Cheers
Lee
 
That is correct you will leave the script tags off
of the external file.

2b||!2b
 
This has honestly got to be the *last* question!

I have just one checkbox that I need to ensure is checked. What would the syntax be? I have tried this, but it just ignores it completely. I guess I can't say value.length < 1?

(f.ContractedInIndicator.value.length < 1)
? showImage(&quot;ContractedInIndicator&quot;, &quot;images/fieldempty.gif&quot;, true) // no semi-colon after this line!
: showImage(&quot;ContractedInIndicator&quot;, &quot;images/blankimage.gif&quot;, false);

While I am asking - would the solution apply for radio buttons and drop downs?

Thanks, Lrnmore.
 
I'm also getting the following error only when my server side validation kicks in and prints a page saying 'not completed..' User then clicks the back button and all the values are posted back into the form. If user then completes the form properly and clicks submits, I get this error

exception of type &quot;Microsoft JScript runtime error: 'document [...]' is null or not an object was not handled

If I debug it takes me to this code and highlights
this : document[imagename].src = imageurl; and when I hover over this line it says &quot;imageurl= images/blankimage.gif&quot;

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

The images weren't in a folder originally. I then placed them in an images folder and changed all the paths to /images. Do you think this is why it's doing this?

I am almost there and your help has been invaluable! This has taken me days..
Thanks
Lee
 
Well,

>>the following error only when my server side validation kicks in and prints a page saying 'not completed..'

Is there something left undone that let's the form
be submitted, if we are doing your cks correctly it
should not get that far.

One test I can think of for the error, if you deliberately
throw an error does it pull the images correctly??

Try to pull them all....

Is there a reason you don't want to use alerts like
everybody else?

I wonder if something is happening to the cache when
focus is returned to the page after the error.

And as far as checking the state of a checkbox:
if(document.forma.ckboxa.checked == true){
alert(&quot;Checked&quot;); }

Radio buttons have to use an index.

Check out my newly published but not ready for
syndication form tool at:


It will hopefully give you an idea or two about
ref to form elements.


2b||!2b
 
:) 2b||!2b

Your code looks interesting. Will it perform all checks? It's a nightmare to find a script that performs all types of checks. Most of them only do 2 or 3 checks and then you're left trying to figure out how to do the rest. Will you be posting an example once it's complete? Always helps to be able to test run it to see exactly how it works. Looks like I've been asking the right person re all this!

All the checks are now performing perfectly and all images appear. However, if someone has javascript turned off then the asp server side validation will kick in and check that the fields are not empty. It retrieves and stores the values from the form and then 'on click' posts them back to original form where I display another form (exactly the same) but with these posted values set as the values. It's in here the error occurs. Do you think it could be the cache?

I also noticed with the line which the debugger highlights as an error

document[imagename].src = imageurl;

maybe it's something to do with 'document'? I have tried 'window.document' but still an error. It must be either the document part or the imageurl part. Does this ring any bells?

Thanks!
Lee

 
well duhhh, this could be the prob. try:

document.images[imagename].src = imageurl;

It will work better this way I'm sure.

If not, since your images work correctly the first load, I'm
thinking something has to be amiss with the focus
return.

Can you just use alerts and be like everybody else??



2b||!2b
 
Thanks for sticking with me on this.

That was it - it's now working..

I originally went for the script because it performed more checks than other scripts I saw. It's been well worth it though as the way it validates looks amazing - very cool. Take a look at the orginal example:
MUCH APPRECIATED!
Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top