By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 995 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

Form Validation help

P: n/a
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 ==
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw xyz")
{
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

Apr 4 '06 #1
Share this Question
Share on Google+
14 Replies


P: n/a
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 :)

Apr 4 '06 #2

P: n/a
JN*****@gmail.com wrote:
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">
<script type="text/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);
Why not valid = false? Makes the function flow better.


else if (document.ITTermination.EmployeeName.value ==
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw xyz")


This is an exact compare, what you want is a regular expression match,
try something like value.match( \^[a-zA-Z]$\ ).

Read up on regular expressions, they are a powerful tool for validating
strings.

--
Ian Collins.
Apr 4 '06 #3

P: n/a
JN*****@gmail.com 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:

<script type="text/javascript">


<!--
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.

[...]
--
Rob
Group FAQ: <URL:http://www.jibbering.com/FAQ>
Apr 5 '06 #4

P: n/a
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

Apr 5 '06 #5

P: n/a
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

Apr 5 '06 #6

P: n/a
JN*****@gmail.com wrote:

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...??

Try a simple one liner validate_form() { return false; }

to make sure you are calling the right function, as Lee said, on onsubmit.

--
Ian Collins.
Apr 5 '06 #7

P: n/a
JRS: In article <rJ******************@news.optus.net.au>, dated Tue, 4
Apr 2006 23:57:11 remote, seen in news:comp.lang.javascript, RobG
<rg***@iinet.net.au> posted :
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).

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Apr 5 '06 #8

P: n/a
Dr John Stockton said:
JRS: In article <rJ******************@news.optus.net.au>, dated Tue, 4
Apr 2006 23:57:11 remote, seen in news:comp.lang.javascript, RobG
<rg***@iinet.net.au> posted :
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.


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. :-)
--
Rob
Group FAQ: <URL:http://www.jibbering.com/FAQ>
Apr 6 '06 #9

P: n/a
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

Apr 6 '06 #10

P: n/a
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 :)

Apr 7 '06 #11

P: n/a
JN*****@gmail.com 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.

[...]

--
Rob
Group FAQ: <URL:http://www.jibbering.com/FAQ>
Apr 7 '06 #12

P: n/a
You guys (besides Marty2) make me feel like never posting here again.

Thanks a lot

Apr 7 '06 #13

P: n/a
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

Apr 7 '06 #14

P: n/a
JRS: In article <11**********************@v46g2000cwv.googlegroups .com>
, dated Thu, 6 Apr 2006 12:44:28 remote, seen in
news:comp.lang.javascript, JN*****@gmail.com <JN*****@gmail.com> posted
:
Alot has been learned here. And yes, you are discouring because I am a
newbie.
...


So you should read the newsgroup FAQ before posting - all sections. If
you had done that, you could have avoided many errors.

Then read <URL:http://www.merlyn.demon.co.uk/js-valid.htm>.

Then read the FAQ again.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Apr 7 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.