473,396 Members | 2,004 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,396 software developers and data experts.

Javascript field validation

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
4 1565
JRS: In article <ee**************************@posting.google.com >,
dated Tue, 25 Jan 2005 01:53:28, seen in news:comp.lang.javascript,
Colin Graham <cs********@hotmail.com> posted :
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.
This checks a single field. Your question is unclear as to whether
field 1 = 11111 and field 2 = 22222 is good or bad. Checking the
concatenation of your fields does what you might want.

function Try(S) {
Bad = /^(.)(\1)+$/.test(S.value) // 1st character is followed by sames
if (Bad) { alert('Aaarrrgh!') ; S.focus() }
return !Bad }

Note - that accepts having only one character, which is necessarily all
the same; or none. Consider changing + to *.
OK = /(.)[^\1])/.test(S.value) // does not work, but
someone may be able to write a RegExp that tests only
for an instance of a character followed by a
different one.

See <URL:http://www.merlyn.demon.co.uk/js-valid.htm>.
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.


In most cases, only if you consider them as probably bad examples - of
course, one can still learn something from them.
--
© 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.
Jul 23 '05 #2
rh
Dr John Stockton wrote:
JRS: In article <ee**************************@posting.google.com >,
dated Tue, 25 Jan 2005 01:53:28, seen in news:comp.lang.javascript,
<...>


OK = /(.)[^\1])/.test(S.value) // does not work, but
someone may be able to write a RegExp that tests only
for an instance of a character followed by a
different one.


Appears to have an extraneous ")", but I think you're right that a
backreference cannot be used there.

This perhaps?:

| OK = /^(.)(?!\1+$)/.test(S.value)
<...>

../rh

Jul 23 '05 #3
rh wrote:
Dr John Stockton wrote:
JRS: In article <ee**************************@posting.google.com >,
dated Tue, 25 Jan 2005 01:53:28, seen in news:comp.lang.javascript,
<...>


OK = /(.)[^\1])/.test(S.value) // does not work, but someone may be able to write a RegExp that tests only for an instance of a character followed by a
different one.


Appears to have an extraneous ")", but I think you're right that a
backreference cannot be used there.


Not in a negated character class, anyway.

This perhaps?:

| OK = /^(.)(?!\1+$)/.test(S.value)


(snip)

Why would a zero-width assertion be needed?

<!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()
{

var fchk = {
'val1' : 'value 1' ,
'val2' : 'value 2'
};

var f, ftop, msg = '';
for (fid in fchk)
{
f = document.getElementById(fid);
f.value = f.value.replace(/\s/g, '');
if (/^$/.test(f.value))
{
msg += fchk[fid] + ': blank\n';
ftop = ftop || f;
}
else if (/^(.)\1+$/.test(f.value))
{
msg += fchk[fid] + ': same characters\n';
ftop = ftop || f;
}
}
if (msg != '')
{
var bef = 'Errors noted:\n\n';
var aft = '\nPlease correct.\n';
alert(bef + msg + aft);
if (ftop && ftop.focus)
{
ftop.focus();
ftop.select();
}
return false;
}
return true;
}

</script>
</head>
<body>
<form onsubmit="return checkit()">
<input id="val1" type="text" name="val1" value="" />__value 1
<br />
<input id="val2" type="text" name="val2" value="" />__value 2
<hr />
<input type="submit" value="done" />
</form>
</body>
</html>

Jul 23 '05 #4
rh
RobB wrote:
rh wrote:
Dr John Stockton wrote:
JRS: In article <ee**************************@posting.google.com >, dated Tue, 25 Jan 2005 01:53:28, seen in
news:comp.lang.javascript,

<...>


OK = /(.)[^\1])/.test(S.value) // does not work,
but someone may be able to write a RegExp that tests only for an instance of a character followed by a
different one.


Appears to have an extraneous ")", but I think you're right that a
backreference cannot be used there.


Not in a negated character class, anyway.


Nor in a non-negated character class, if I recall the semantics
correctly.

This perhaps?:

| OK = /^(.)(?!\1+$)/.test(S.value)


(snip)

Why would a zero-width assertion be needed?


Uh, to make it work? ;-) (Note that it does not fully meet JRS's
suggestion of providing a test for differing adjacent characters.)
<..>

../rh

Jul 23 '05 #5

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

Similar topics

5
by: Alex M | last post by:
I'm trying to use the jakarta struts client side javascript validators and the validators are working, but they are not showing the popup messages. I have the message keys defined in the...
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: Dave Blair | last post by:
Hi, I have a problem with our intranet, we are planning to install Firefox instead of Internet Explorer onto some new PCs. However, we can't get the following JavaScript to work in Firefox and...
4
by: Madha K | last post by:
I am developing a web application that need to support UTF-8 characters. For client side validations, can javascript be used to handle UTF-8 characters. 1) How javascript can be used to restrict...
7
by: mhk | last post by:
Hi, Is there any way to create/set Session veriable in JavaScript. Please let me know if anyone has an idea. Thanks alot.
5
by: billa | last post by:
I wish to carry out standard form validation (i.e. is it a date?, is there a value in the field, is it a number) using the onBlur event rather than the onSubmit event. This (of course) leads to...
2
by: techfuzz | last post by:
I scoured this group and others looking for the best way to disable a button after the first click to prevent multiple submissions, but never did find anything that worked like they said it would. ...
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...
1
by: fran7 | last post by:
hi, I have two javascript form validation scripts. Although the forms have different names, one of the scripts overrides the other and validates both. They have the same onsubmit, and one has a...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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
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,...

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.