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

Javascript field validation

P: n/a
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
Share this Question
Share on Google+
4 Replies


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

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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.