473,395 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Javascript Validate Form Fields

Hi there,

Im very new to javascript and this is my first bit of javascript
coding. I have to write a validation check for two fields that they
don't contain all the same characters e.g.,

field 1 = 111111 field 2 = 11111111

OR

field 1 = 000000 field 2 = 00000000

my script wants to check to see if this is happening and display an
error message.

I would also be agreatful if anyone could point me towards any
javascript code banks on the web as this would help speed up my
learning.

any help greatly appreciated.

Thanks

Colin
Jul 23 '05 #1
1 1616
Colin Graham wrote:
Hi there,

Im very new to javascript and this is my first bit of javascript
coding. I have to write a validation check for two fields that they
don't contain all the same characters e.g.,

field 1 = 111111 field 2 = 11111111

OR

field 1 = 000000 field 2 = 00000000

my script wants to check to see if this is happening and display an
error message.

I would also be agreatful if anyone could point me towards any
javascript code banks on the web as this would help speed up my
learning.

any help greatly appreciated.

Thanks

Colin


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>untitled</title>
<script type="text/javascript">

function checkit(els)
{
//get field
var field = els.something;
//strip whitespace
field.value = field.value.replace(/\s/g, '');
//empty?
if (/^$/.test(field.value))
{
alert('Please enter something.');
field.focus();
return false;
}
//get first character
var first = field.value.match(/^./);
if (first)
{
//make it a regular expression
var re = new RegExp(first[0], 'g');
//remove all instances of it, test for empty field
if (/^$/.test(field.value.replace(re, '')))
{
alert('Entries must not consist of all the same character.');
field.focus();
field.select();
//abort submit
return false;
}
//pro-life
return true;
}
}

</script>
</head>
<body>
<!-- call validator, pass elements object -->
<form onsubmit="return checkit(this.elements)">
<input type="text" name="something" value="" />
<input type="submit" />
</form>
</body>
</html>

Studying 'code banks' (imo) will slow down, not speed up your progress.
You'll need to read some good tutorials/textbooks and progress step by
step. Here's a good one:

http://www.amazon.com/gp/reader/0072...18#reader-link
http://www.regular-expressions.com/tutorial.html

Jul 23 '05 #2

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

Similar topics

8
by: Sue | last post by:
Hello! I am back with another question. Remember I am a new JavaScript student and I am aware that this code does not check for all the possibilities and that as a "NEW" JavaScript student I am...
8
by: Sue | last post by:
In this code why is it that when I press the SUBMIT button the focus only goes back to the Numeric field. What do I need to do to correct this problem? Sue <html>
5
by: Sue | last post by:
After finishing up my first quarter JavaScript on 12/12/03, I decided to improve character checking on my project. In my project I only had to do very basic validation. Therefore, I only had one...
2
by: GIMME | last post by:
Background ... I've created a web application that allows a user to create an HTML application from IE. The application itself creates an XML representation of a XHTML form. The XHTML...
19
by: dmiller23462 | last post by:
Hi guys....I have absolutely NO IDEA what I'm doing with Javascript but my end result is I need two text boxes to stay hidden until a particular option is selected....I've cobbled together the...
1
by: IkBenHet | last post by:
Hello, Currently I am using a large input form on a website that is based on ASP and JavaScript. Depending on the values that are filled in by the user the forms does a refresh and makes...
7
by: amerar | last post by:
Hi All, I am really green at Javascript. Someone gave me some code to play with and I'm trying to understand it: <INPUT TYPE=text NAME=subject SIZE=60 MAXLENGTH=60><BR><BR> <input...
11
by: Marcelo | last post by:
Hi, I need to make a robot that fills forms. I already made that with php sites, but now I need that in a javascript pages site ( the robot can be php,asp, asp.net ), anyone knows how can I do...
3
by: pragan | last post by:
HI, I need to create an online form with Details such as Name, Address, EMail, Phone, Message, Credit Card Type/Number/Expiry Date. I started this and I was able to create the form and validate...
2
by: Mick Walker | last post by:
Hi, I have a problem that I have been trying to figure for a couple of days, and am wondering if anyone out there would be so kind as to give me a solution. (Deadline time) I am trying to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.