Form Validation help

J

JNariss

Hello,

I am fairly new to asp and jscript and am now in the process of
learning some form validation. I am taking one step at a time by
validating each field and testing it before moving onto the next one to
be sure I am correct. I ran into a problem with my validation when I
added an else if code to my code.

Here is what I tried to do: Form (ITTermination) has a field
(EmployeeName) which I would like to validate to check for no value and
alpha characters A - Z only, no numbers.

This code worked great to ONLY to validate a no value in the field:

<script language="JavaScript">

<!--

function validate_form ( )
{
valid = true;

if ( document.ITTermination.EmployeeName.value == "" )
{
alert("Please enter first and last name for 'Employee Name'
using only characters." );
document.ITTermination.EmployeeName.focus();
return (false);
}
return valid;
}

//-->

</script>



Here is the text label info with the validation check included in the
onBlur event:

<label>Employee Name:
<input name="EmployeeName" type="text" id="EmployeeName"
style="background-color: #C0C0C0" size="35" maxlength="35"
onBlur="validate_form();"/>
</label>


So when I got daring I decided to check that no numbers are entered
into the field and came up with this code which is not working:

<script language="javascript">
<!--
function validate_form () {
valid = true;
if (document.ITTermination.EmployeeName.value == "" )
{
alert("Please enter first and last name for 'Employee Name' using
only characters." );
document.ITTermination.EmployeeName.focus();
return (false);
}

else if (document.ITTermination.EmployeeName.value ==
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
{
alert("Please do not use numbers within the Employee Name field. Only
alphabetic characters are allowed.")
document.ITTermination.EmployeeName.focus();
return (false);
}
return valid;
}
//-->
</script>


I even tried using else if (document.ITTermination.EmployeeName.value
== "alpha") and that did not work either.

If someone would be so kind to help me out I will be on my way to the
next field. I would like to keep adding validations to this form within
the one script validate_form( ).

Thanks in advance,
Justine
 
M

Martyr2

Your else if statement there is asking does the value equal that entire
string of letters. Which is not the case unless they entered all those
characters. What you need is a function that will return true or false
if the value is a number or not.

That function is called isNaN() which stands for "Is Not a Number" and
it works like this....

isNaN(document.ITTermination.EmployeeName.value)

This will return true only if the value is all letters, otherwise it
will return false saying that it has a number in it somewhere. Now this
may not be the solve it all solution because I am not quite sure how it
will handle all special characters, so you might want to also find a
custom made function (which probably will use indexOf function) to
offer complete fool proof validation.

However, isNaN should help you quite a bit and in the right direction.
Hope this helps :)
 
R

RobG

(e-mail address removed) said on 05/04/2006 5:22 AM AEST:
Hello,

I am fairly new to asp and jscript and am now in the process of
learning some form validation. I am taking one step at a time by
validating each field and testing it before moving onto the next one to
be sure I am correct. I ran into a problem with my validation when I
added an else if code to my code.

Here is what I tried to do: Form (ITTermination) has a field
(EmployeeName) which I would like to validate to check for no value and
alpha characters A - Z only, no numbers.

This code worked great to ONLY to validate a no value in the field:

<script language="JavaScript">

The language attribute is deprecated, type is required:


Do not use HTML comments inside script elements, they are useless.

function validate_form ( )
{
valid = true;

if ( document.ITTermination.EmployeeName.value == "" )
{
alert("Please enter first and last name for 'Employee Name'
using only characters." );

Don't allow posted code to auto-wrap, manually wrap code at about 70
characters.

alert("Please enter first and last name for "
+ "'Employee Name' using only characters." );

And instead of 'characters', use 'letters'. Characters can be anything
- letters, digits, punctuation, and so on.

[...]
Here is the text label info with the validation check included in the
onBlur event:

<label>Employee Name:
<input name="EmployeeName" type="text" id="EmployeeName"
style="background-color: #C0C0C0" size="35" maxlength="35"
onBlur="validate_form();"/>

It is nasty to validate using onblur in conjunction with an alert and
returning focus to the control. It traps users so that they can't
escape until they enter 'valid' data into the input. They may have a
perfectly good reason to leave the field invalid for the time being, so
validate the form on submit or write your error message in the document
and let the user continue.

And is really XHTML? Your attribute names suggest not, so ditch the
pseudo-XML '/>', just use '>'.

If you persist with onblur, you can pass a reference to the control
directly to the validate_form() function using 'this':

... onblur="validate_form(this);">


Now your function can be:

function validate_form (input)
{
// input is a reference to the input that
// called the function
}
</label>


So when I got daring I decided to check that no numbers are entered
into the field and came up with this code which is not working:
[...]

A simple function to check if any digits have been entered:

<script type="text/javascript">

function hasDigits(x){ return /\d/.test(x);}

function validate_form (input)
{
if ( hasDigits(input.value) ){
alert('Digits in the value...');
}
}
</script>

<input type="text" onblur="validate_form(this);">


You may want to play with some of Matt Kruse's stuff here:

<URL:http://www.mattkruse.com/javascript/validations/>

He lurks here from time-to-time, so ask if you have any questions.

[...]
 
J

JNariss

Wow.......this is incredible and I appreciate all your help. I will go
to work in the morning and adjust and test my code. Please be sure that
I may be back, as I have not only one but a second form (even more
complicated) to validate.

-Justine
 
J

JNariss

Here is what I changed the script code to:

<script language="text/javaScript">
function validate_form(input)
{
valid = true;
if ( document.ITTermination.EmployeeName.value == "" )
{
alert("Please enter first and last name for "
+ "'Employee Name' using only letters." );
valid = false;
}

function hasDigits(x)
{
return /\d/.test(x);
}

function validate_form(input)
{
if ( document.ITTermination.EmployeeName.value =
hasDigits(input.value) ) {
alert("The values entered into 'Employee Name' can only consist of
letters");
}
Valid = false;
}
return valid;
}
</script>



And then took out the onBlur within the field input code and added the
onClick to the submit button: <input type="submit" name="Submit"
value="Submit" onClick="validate_form(this);">


Unfortunatley it is still not working. I feel that the problem may be
how I am adding each function into the script. And I noticed that I
used function validate_form() twice. One for the "" empty field value
and one for the hasDigits value. My gut tells me this is not right -
but then again I have no idea.

Any ideas...??

Thanks,
-Justine
 
L

Lee

(e-mail address removed) said:
Here is what I changed the script code to:

<script language="text/javaScript">
function validate_form(input)
{
valid = true;
if ( document.ITTermination.EmployeeName.value == "" )
{
alert("Please enter first and last name for "
+ "'Employee Name' using only letters." );
valid = false;
}

function hasDigits(x)
{
return /\d/.test(x);
}

function validate_form(input)
{
if ( document.ITTermination.EmployeeName.value =
hasDigits(input.value) ) {
alert("The values entered into 'Employee Name' can only consist of
letters");
}
Valid = false;
}
return valid;
}
</script>



And then took out the onBlur within the field input code and added the
onClick to the submit button: <input type="submit" name="Submit"
value="Submit" onClick="validate_form(this);">


Unfortunatley it is still not working. I feel that the problem may be
how I am adding each function into the script. And I noticed that I
used function validate_form() twice. One for the "" empty field value
and one for the hasDigits value. My gut tells me this is not right -
but then again I have no idea.

I don't like to be discouraging, but you're really never going
to accomplish much by blindly writing code that you don't
understand.

Never validate in the onclick handler of the submit button.
Use the onsubmit handler of the form, or, if you want to
validate each field as it is entered, use that field's
onchange handler.

Since your function is called "validate_form", I assume you
want it to validate all of the fields, so it should be called
from the form's onsubmit handler and should return true or
false to indicate whether the fields were all valid.
This value is, in turn, returned by the onsubmit handler, to
tell the browser whether or not the submission should be
allowed to proceed:

<form action="..." onsubmit="return validate_form(this)">

In this usage, the value "this" being passed to the function
is a reference to the Form object.


function validate_form(thisForm)
{
var valid = true;

if ( -1 == thisForm.EmployeeName.value.search(/\S/) )
{
// value does not contain any non-blank characters
valid = false;
alert( "Please do not leave Employee Name blank." );
}
else if ( -1 == thisForm.EmployeeName.value.search(/^[A-Z ]+$/i) )
{
// value is not entirely letters and spaces
valid = false;
alert( "Please enter only letters and spaces for Employee Name." );
}
return valid;
}


--
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Tue, 4
Apr 2006 23:57:11 remote, seen in RobG
alert("Please enter first and last name for "
+ "'Employee Name' using only characters." );

And instead of 'characters', use 'letters'. Characters can be anything
- letters, digits, punctuation, and so on.

Characters is correct.

Consider Patrick O'Brian, Henry Babcock-Smythe, Maarten s'Hertogenbosch,
and those from the !Kung people of southern Africa. Also, I've had two
independent friends whose surnames contained a space (one has since
hyphenated).
 
R

RobG

Dr John Stockton said:
JRS: In article <[email protected]>, dated Tue, 4
Apr 2006 23:57:11 remote, seen in RobG



Characters is correct.

That is a rather definitive response to a fairly vague circumstance, for
one normally quite precise.

Consider Patrick O'Brian, Henry Babcock-Smythe, Maarten s'Hertogenbosch,
and those from the !Kung people of southern Africa. Also, I've had two
independent friends whose surnames contained a space (one has since
hyphenated).

Certainly names can have a variety of characters other than letters,
including punctuation - the most obvious being that 'Employee Name'
would typically consist of at least one space (though some may not).
But the OP requested:

"to validate to check for no value and alpha characters A - Z only,
no numbers."

Now that might have been a mistake, perhaps the real requirement was
something different, but my response was based on what the OP requested.
Hopefully my reiteration of the restriction caused a reconsideration
of the restrictiveness of the requested test.

I would not reject names with numbers or punctuation, but maybe I'd be
suspicious of someone claiming to be William H. Gates 3. :)
 
J

JNariss

Alot has been learned here. And yes, you are discouring because I am a
newbie.

1. I learned that people are characters. And we type letters,
punctuations and numbers

2. I should be more informative of what I have previously tried. ( I
tried to validate onsubmit, onblur, and onclick - nothing was working
so I decided to post)

3. People are quick to view a question and reply to someone elses post,
yet have no help for the original post

4. My validation does not work

5. My original posted code revealed that this worked:

<script language="JavaScript">
function validate_form ( )
{
valid = true;
if ( document.ITTermination.EmployeeName.value == "" )
{
alert("Please enter first and last name for 'Employee Name' using
only characters." );
document.ITTermination.EmployeeName.focus();
return (false);
}
return valid;
}
</script>


6. I then posted what I changed and how it did not work

7. The real requirement is a need to add validation to the field
"EmployeeName" so it does not contain numbers 0 - 9

I have tried researching but thought it may be quicker to ask people
who most likely know the answers. I have a basic understanding, but
need a little help along the way.

-Justine
 
M

Martyr2

Hi Justine,

I feel a bit sorry for you and not getting the help you were looking
for at the time. I shall remedy the situation with the code below. This
uses the regular expression method and I tried to comment it to help
you understand it.

<script language="javascript">
<!--
function validate_form () {
valid = true;

// Regular expression saying only accept a string that contains
letters, a dash, an apostrophe or a space, anything else violates it.

var objReg = /^[a-zA-Z\-'\s]+$/

if (document.ITTermination.EmployeeName.value == "" )
{
alert("Please enter first and last name for 'Employee
Name' using only characters." );
document.ITTermination.EmployeeName.focus();
return false;
}

else if
(!objReg.test(document.ITTermination.EmployeeName.value))
{
// This code executes if the regular expression returns false
(meaning they typed a number of special character not allowed by our
expression.)
alert("Please do not use numbers or special characters
other than a dash or apostrophe within the Employee Name field. Only
alphabetic characters are allowed.")
document.ITTermination.EmployeeName.focus();
return false;
}
return valid;
}

//-->
</script>

</head>

<body>

<form name="ITTermination" method="POST" action="test.html"
onSubmit="return validate_form()">

<input type="text" name="EmployeeName">
<input type="submit" name="submit" value="Submit">
</form>

I have included a little test form so that you could cut and paste this
entire thing into a html file and test it for yourself. I hope this
becomes the definitative answer to the question. Good luck :)
 
L

Lee

(e-mail address removed) said:
Alot has been learned here. And yes, you are discouring because I am a
newbie.

Being a newbie doesn't excuse just randomly trying things that
you don't understand. Read a manual.
3. People are quick to view a question and reply to someone elses post,
yet have no help for the original post

Yes, we call it USENET.

My previous response included a working, tested validation
function that you could plug into your page. You've got to
at least be able to recognize help when you receive it.


--
 
R

RobG

(e-mail address removed) said on 07/04/2006 5:44 AM AEST:
Alot has been learned here. And yes, you are discouring because I am a
newbie.

1. I learned that people are characters. And we type letters,
punctuations and numbers

Which are all characters.


[...]
3. People are quick to view a question and reply to someone elses post,
yet have no help for the original post

I (amongst others) posted help in my first reply to your original post.
That others continue the discussion on related matters is usually
considered a bonus.

4. My validation does not work

There are several posts that provide working examples of how to go about it.

[...]
 
J

JNariss

And for your information I was thrown into this job and we have an
audit to get through on time and they need to see that this form works
and that the information is going into a database. I could have easily
created the form and not had validation on it but I thought that that
may not be a very good idea. So I looked into adding validation. I even
went to the library and got a "Guide to building intelligent websites
JavaScript" and downloaded manuals on JavaScript and form validation.

Well I am not going to fight about this with people I don't even know.
I appreciate everyones help and in reality I will have to look
elsewhere for help b/c I will not come to work every morning in hopes
that I can continually ask questions here and receive answers.

I have never had a problem posting here until now. Actually until I
posted in this group!!!!

And by the way, Marty2, thank you kindly for your help. I will try the
provided example in my form and take it from there.

-Justine
 
L

Lee

(e-mail address removed) said:
You guys (besides Marty2) make me feel like never posting here again.

This is a techinical newsgroup.
We pointed out your mistakes and posted solutions.
We're not here to support you emotionally.
Maybe there are other newsgroups for that.


--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top