Javascript field validation

C

Colin Graham

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
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Tue, 25 Jan 2005 01:53:28, seen in
Colin Graham said:
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.
 
R

rh

Dr said:
JRS: In article <[email protected]>,
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
 
R

RobB

rh said:
Dr said:
JRS: In article <[email protected]>,
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>
 
R

rh

RobB said:
rh said:
Dr said:
JRS: In article
dated Tue, 25 Jan 2005 01:53:28, seen in
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.
(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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top