472,958 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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="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
14 2087
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
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
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
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(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
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
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
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
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
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
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
You guys (besides Marty2) make me feel like never posting here again.

Thanks a lot

Apr 7 '06 #13
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
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...
6
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...
4
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...
9
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...
27
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...
11
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...
5
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...
0
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...
3
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...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.