469,951 Members | 2,514 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,951 developers. It's quick & easy.

Text Validation?

al
Greetings,

I trying to validate user input to only alphabet [a-zA-Z] or '\w', but
it is not working for me.

Code:

if (document.all("txtName").value == [a-zA-Z])

//valiation is OK..Rest of code

else{

alert("Please enter only alphabet!");
document.all("txtName").value == "";
}

MTIA,
Grawsha
Jul 23 '05 #1
9 3597
gr*********@yahoo.com (al) writes:
I trying to validate user input to only alphabet [a-zA-Z] or '\w', but
it is not working for me.

Code:

if (document.all("txtName").value == [a-zA-Z])
document.all is a proprietary Microsoft invention and
doesn't work in all browsers. You could use
document.forms['formName'].elements["txtName").value
instead.

The "expression" [a-zA-Z] is parsed as :
A list containing a subtraction of the variables "zA" and "Z"
from the variable "a".
Probably not what you meant :).

To test for an all-alphanumeric string, use a regular expression:

if( /^\w*$/.test(someString) ) ...
or
if ( someString.match(/^\w*$/) ) ...

Now put the reference to the form control value into the match :)
alert("Please enter only alphabet!");
document.all("txtName").value == "";


I recommend against clearing the field. If the user had a long input
with a single error, he will have to write it from scratch instead
of just fixing the error. Highly annoying. Instead, I would put focus
on the element, and perhaps even select the contents:

var elem = document.forms['formName'].elements['txtName'];
elem.focus();
elem.select();

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #2
You must use reg exps, which is what you are doing but your codes needs
some tidying up. Also, you are using IE specific javascript which will
render your code non-DOM compliant, so I fixed up the references.

// created 2 reg exp object instances

var exprOne = /[a-zA-z]/;
var expTwo = /[0-9]/;

var data = document.FormName.txtName.value; // don't use document.all,

// test for text only

if(exprOne.test(data) && !expTwo.test(data)){

// valiation is OK..Rest of code

} else {

document.FORMNAME.txtName.value = "";

}

Cycloneous

al wrote:
Greetings,

I trying to validate user input to only alphabet [a-zA-Z] or '\w', but
it is not working for me.

Code:

if (document.all("txtName").value == [a-zA-Z])

//valiation is OK..Rest of code

else{

alert("Please enter only alphabet!");
document.all("txtName").value == "";
}

MTIA,
Grawsha

Jul 23 '05 #3
In article <66**************************@posting.google.com >,
gr*********@yahoo.com enlightened us with...
Greetings,

I trying to validate user input to only alphabet [a-zA-Z] or '\w', but
it is not working for me.

Code:

if (document.all("txtName").value == [a-zA-Z])


IE only?
Not so good.

function isAlpha(str)
{
var re = /^[A-Za-z ]+$/;
return re.test(str);
}

if (! isAlpha(document.forms["formname"].elements["elementname"].value))
{
alert("wrong");
return false;
}
--
--
~kaeli~
The secret of the universe is @*&^^^ NO CARRIER
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #4
Cycloneous Echevarria wrote:
You must use reg exps, which is what you are doing but your codes needs
some tidying up. Also, you are using IE specific javascript which will
render your code non-DOM compliant, so I fixed up the references.


No, you do not "must" use reg exps. I can, very simply, write an over
bloated function that will check for a-z and A-Z and never use a regex.
Yes, its a lot more efficient but is *not* required, which is what your
"must" implied.

As for "non-DOM compliant", thats balderdash. document.all is *very*
"DOM compliant" with the DOM in IE, its just not compliant with other
DOM's in other browsers/UA's.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/
Jul 23 '05 #5
Randy Webb <hi************@aol.com> writes:
As for "non-DOM compliant", thats balderdash. document.all is *very*
"DOM compliant" with the DOM in IE, its just not compliant with other
DOM's in other browsers/UA's.


That makes any feature (or bug) "DOM compliant" on the version of the
browser it runs on :)
But yes, for precission, it is not W3C DOM compliant, which is the only
non-browser-specific DOM.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #6
Lasse Reichstein Nielsen wrote:
Randy Webb <hi************@aol.com> writes:

As for "non-DOM compliant", thats balderdash. document.all is *very*
"DOM compliant" with the DOM in IE, its just not compliant with other
DOM's in other browsers/UA's.

That makes any feature (or bug) "DOM compliant" on the version of the
browser it runs on :)


Precisely.
But yes, for precission, it is not W3C DOM compliant, which is the only
non-browser-specific DOM.


I am still not convinced that writing "W3C DOM compliant" code is all
that great. Yes, it goes by the spec but if its not implemented in the
Browser and/or UA, then its still worthless :)

Code that works is still better than code that is "compliant". Yanno?

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/
Jul 23 '05 #7
Randy Webb <hi************@aol.com> writes:
I am still not convinced that writing "W3C DOM compliant" code is all
that great. Yes, it goes by the spec but if its not implemented in the
Browser and/or UA, then its still worthless :)
I'll agree if you mean the browser is worthless :)

Writing *only* W3C DOM compliant code will not work, since there are
still worthless browsers in wide use. Writing W3C DOM compliant code
as the primary branch, and then having fallbacks for non-compliant
browsers, is the safest way to script. It has the advantage of working
with any new browser that appears, since they are bound to be W3C DOM
compliant (or at least close). No other way of scripting will give you
forwards compatability (effectively demonstrated by all the "it works
in IE but not in Netscape, what should I do" posts).
Code that works is still better than code that is "compliant". Yanno?


And code that works both today and tomorrow is better than code that
only works today. At least if I have to maintain it :)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #8
Lasse Reichstein Nielsen wrote:
Randy Webb <hi************@aol.com> writes:

I am still not convinced that writing "W3C DOM compliant" code is all
that great. Yes, it goes by the spec but if its not implemented in the
Browser and/or UA, then its still worthless :)

I'll agree if you mean the browser is worthless :)


That depends. I find it a lot easier to dynamically load js files in IE
than any other browser. Simply because it supports the proprietary
method of changing the .src of a script tag with an id. The advantage is
that you aren't continually adding script elements to the document, as
you do when using createElement, although createElement is "W3C DOM".

Which way I do something, whether proprietary then W3C, or W3C and then
proprietary, depends directly on which one is more efficient. Sometimes,
its more efficient to use the proprietary features.

My, or anyone elses, opinion of IE aside, its simply a lot simpler to
script for than other browsers, if for no other reason than its
tolerance of errors.
Writing *only* W3C DOM compliant code will not work, since there are
still worthless browsers in wide use. Writing W3C DOM compliant code
as the primary branch, and then having fallbacks for non-compliant
browsers, is the safest way to script. It has the advantage of working
with any new browser that appears, since they are bound to be W3C DOM
compliant (or at least close). No other way of scripting will give you
forwards compatability (effectively demonstrated by all the "it works
in IE but not in Netscape, what should I do" posts).


Being "W3C DOM Compliant" does not make a browser "non-worthless" nor
does being non-Compliant make it worthless.
Code that works is still better than code that is "compliant". Yanno?

And code that works both today and tomorrow is better than code that
only works today. At least if I have to maintain it :)


That still doesn't always make "compliant code" the most efficient nor
the easiest to maintain. It can be written either way, whether it goes
like this:

if (document.all){

}else
{if (document.getElementById){

}
}
or this:

if (document.getElementById){

}else
{if (document.all){

}
}

It will *still* work tomorrow, the difference is in efficiency. So I
guess I should have said "Efficient proprietary code is better than
less-efficient W3C Dom Compliant code" and I prefer efficiency to
"compliance".
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/
Jul 23 '05 #9
JRS: In article <Vu********************@comcast.com>, seen in
news:comp.lang.javascript, Randy Webb <hi************@aol.com> posted at
Mon, 10 May 2004 16:40:58 :

My, or anyone elses, opinion of IE aside, its simply a lot simpler to
script for than other browsers, if for no other reason than its
tolerance of errors.


If one is able to choose the browser that the readers of one's stuff
will use - which is possible if one is an IT manager with an intranet,
but is unreasonable when authoring for the Web - then it is expedient to
choose a tolerant browser and enable shoddy work. There's no guarantee,
of course, that the next issue of the same browser will give the same
result with incorrect but tolerated code.

But a Web author, writing for a diversity of browsers, is best helped by
using a strict browser in design and authoring; ideally, one would never
need to use HTML validators (or accessibility testers), since those
functions would be incorporated in the development browser itself.

A Web author - the default assumption here - should not write "for a
specific browser".

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for 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 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Otto Krüse | last post: by
1 post views Thread by cga1982 | last post: by
6 posts views Thread by The Eeediot | last post: by
1 post views Thread by Joel Barsotti | last post: by
3 posts views Thread by den | last post: by
2 posts views Thread by devnew | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.