473,837 Members | 1,579 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Form Validation help

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="JavaS cript">

<!--

function validate_form ( )
{
valid = true;

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

//-->

</script>

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

<label>Employ ee Name:
<input name="EmployeeN ame" type="text" id="EmployeeNam e"
style="backgrou nd-color: #C0C0C0" size="35" maxlength="35"
onBlur="validat e_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="javas cript">
<!--
function validate_form () {
valid = true;
if (document.ITTer mination.Employ eeName.value == "" )
{
alert("Please enter first and last name for 'Employee Name' using
only characters." );
document.ITTerm ination.Employe eName.focus();
return (false);
}

else if (document.ITTer mination.Employ eeName.value ==
"ABCDEFGHIJKLMN OPQRSTUVWXYZabc defghijklmnopqr stuvwxyz")
{
alert("Please do not use numbers within the Employee Name field. Only
alphabetic characters are allowed.")
document.ITTerm ination.Employe eName.focus();
return (false);
}
return valid;
}
//-->
</script>
I even tried using else if (document.ITTer mination.Employ eeName.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
14 2163
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.E mployeeName.val ue)

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
JN*****@gmail.c om 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="JavaS cript">
<script type="text/javascript">
<!--

function validate_form ( )
{
valid = true;

if ( document.ITTerm ination.Employe eName.value == "" )
{
alert("Please enter first and last name for 'Employee Name'
using only characters." );
document.ITTerm ination.Employe eName.focus();
return (false);
Why not valid = false? Makes the function flow better.


else if (document.ITTer mination.Employ eeName.value ==
"ABCDEFGHIJKLMN OPQRSTUVWXYZabc defghijklmnopqr stuvwxyz")


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
JN*****@gmail.c om 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="JavaS cript">
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.ITTerm ination.Employe eName.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>Employ ee Name:
<input name="EmployeeN ame" type="text" id="EmployeeNam e"
style="backgrou nd-color: #C0C0C0" size="35" maxlength="35"
onBlur="validat e_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="validat e_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="validat e_form(this);">
You may want to play with some of Matt Kruse's stuff here:

<URL:http://www.mattkruse.c om/javascript/validations/>

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

[...]
--
Rob
Group FAQ: <URL:http://www.jibbering.c om/FAQ>
Apr 5 '06 #4
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
Here is what I changed the script code to:

<script language="text/javaScript">
function validate_form(i nput)
{
valid = true;
if ( document.ITTerm ination.Employe eName.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(i nput)
{
if ( document.ITTerm ination.Employe eName.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="valida te_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
JN*****@gmail.c om 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
JRS: In article <rJ************ ******@news.opt us.net.au>, dated Tue, 4
Apr 2006 23:57:11 remote, seen in news:comp.lang. javascript, RobG
<rg***@iinet.ne t.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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Apr 5 '06 #8
Dr John Stockton said:
JRS: In article <rJ************ ******@news.opt us.net.au>, dated Tue, 4
Apr 2006 23:57:11 remote, seen in news:comp.lang. javascript, RobG
<rg***@iinet.ne t.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.c om/FAQ>
Apr 6 '06 #9
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="JavaS cript">
function validate_form ( )
{
valid = true;
if ( document.ITTerm ination.Employe eName.value == "" )
{
alert("Please enter first and last name for 'Employee Name' using
only characters." );
document.ITTerm ination.Employe eName.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
"EmployeeNa me" 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
3932
by: Stefan Richter | last post by:
Hi, after coding for days on stupid form validations - Like: strings (min / max length), numbers(min / max value), money(min / max value), postcodes(min / max value), telefon numbers, email adresses and so on. I thought it might be a better way to programm an automated, dynamic form validation that works for all kinds of fields, shows the necessary error messages and highlights the coresponding form fields.
6
4348
by: Charles Banas | last post by:
weird subject - i hope more than just one curious regular will hear me out. :) ok, i've got a bit of a big problem, and i need answers as soon as possible. i know this forum is meant for web developers, but is relevant discussion. i'm not OT here unless someone thinks i'm trolling (which i'm not, obviously). then i'll disappear and never show my face again. :P
4
2658
by: bnp | last post by:
Hi All, I am quite new the JavaScript. Basically I am a C++ programmer, but now I am working on JavaScript since last 5 days. I have a problem regarding the form validation. I have created a script that validates the form fields. the validation procedure is called ONCLICK event of the submit button. Follwowing is the structure of the validation procedure.
9
4184
by: julie.siebel | last post by:
Hello all! As embarrassing as it is to admit this, I've been designing db driven websites using javascript and vbscript for about 6-7 years now, and I am *horrible* at form validation. To be honest I usually hire someone to do it for me, grab predone scripts and kind of hack out the parts that I need, or just do very minimal validation (e.g. this is numeric, this is alpha-numeric, etc.)
27
4778
by: Chris | last post by:
Hi, I have a form for uploading documents and inserting the data into a mysql db. I would like to validate the form. I have tried a couple of Javascript form validation functions, but it appears that the data goes straight to the processing page, rather than the javascript seeing if data is missing and popping up an alert. I thought it may be because much of the form is populated with data from the db (lists, etc.), but when I leave...
11
3004
by: Rik | last post by:
Hello guys, now that I'm that I'm working on my first major 'open' forms (with uncontrolled users I mean, not a secure backend-interface), I'd like to add a lot of possibilities to check wether certain fields match certain criteria, and inform the user in different ways when the data is wrong (offcourse, this will be checked on posting the data again, but that's something I've got a lot of experience with). Now, offcourse it's...
5
3229
by: lucyh3h | last post by:
Hi, I am trying to use XMLHttpRequest to do server side validation. I have several fields on a form and a submit button. The submit button has an event assocated with it when clicked. The javascript method will do the form validation for each field one by one. For each field, an XMLHttpRequst will be made to a PHP file and get the return, either set an error field (<span>'s innerHTML) or leave it empty. Then I'll check the error field...
0
1343
by: karen987 | last post by:
This is an email form on an ASP page. I want to add validation before it submits, The current validation only checks "name, email, and content" (server side) if the spaces are empty. I need to add the following so that the form is not submitted unless the following is true. 1. The "name" field must be maximum 20 characters. Not left empty. Letters only. 2. The "email" must be a valid email. Not left empty. 3. The "subject" must be maximum...
3
3302
uranuskid
by: uranuskid | last post by:
Hey folks, I was going to include a contact form on my website. Well, in the first place that seemed an easy thing to do with a form that prompts a PHP file validating the input vaiables and using it's mail () function to send the mail. However, as I got more into that topic I realized that one should be really concerned about the validation part to prevent spam abuse. There are shiploads of 'mail scripts' available with each of them has...
0
9851
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10902
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10583
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10286
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9420
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7824
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5680
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4060
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3128
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.