473,410 Members | 1,875 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,410 software developers and data experts.

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 3696
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Otto Krüse | last post by:
Hi everyone, I'm building a GUI in which I want, amongst other things, for people to fill in there postal code. The postal codes of my country (Holland) are in this format: 1234 AB So for the...
1
by: cga1982 | last post by:
I have a text box (for a password) with a validation rule that checks this password against another text box on the form and either lets you continue or tells you that the password is incorrect and...
4
by: Fraggle | last post by:
Hi, I want to validate a text box, The user can leave it blank, or fill it in. If they fill it in then it must be a date within a certain range. How can I achieve this? Thank you very much
6
by: The Eeediot | last post by:
Hello, Folks... I'm almost becoming a regular to this newsgroup. I am trying to display the contents of an MS-SQL Text field to a TextBox in ASPdotNET. The text in this field contains all...
1
by: Stephen Adam | last post by:
Hi there, I have written a custom validation control which checks to see of an input field is not empty and contains only numeric data. I was using a regular expression validation control but...
1
by: Kris | last post by:
Hi, I have a DataGrid where in each row has couple of text boxes and an update button. Each row is dynamically generated as the number of rows are not known ahead of time. When the user clicks the...
1
by: Joel Barsotti | last post by:
Is there anything builtin to ASP.net that allows you to tie a text box to a button so when you press enter in the text box it emulates clicking a near by button. I've coded up some client side...
9
by: sellcraig | last post by:
Microsoft access 2 tables table "data main" contains a field called "code" table "ddw1" is created from a make table query of "data main" Goal- the data in "code" field in needs to...
3
by: den | last post by:
inner a text element if I want to allow the insertion of only: alphabet's letters a,b,c,.... and A,B,C,... number and this - and this _ and not want space blank and others characters what is...
2
by: devnew | last post by:
hi i am new to tkinter and would like some help with canvas i am doing validation of contents of a folder and need to show the ok/error messages on a canvas resultdisplay =Canvas(...)...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.