473,785 Members | 2,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question about whitespace

I've created a function that checks form fields that only will have letters.
This is the script:

<script type="text/javascript" language="javas cript">
function validateString( field, msg, min, max) {
if (!min) { min = 1 }
if (!max) { max = 65535}
if (!field.value || field.value.len gth < min || field.value.max > max) {
alert(msg);
field.focus();
field.select();
return false;
}
return true;
}
</script>

How can I add code to this function that checks for whitespace...ex .
"\n\r\t"
I would also like to check for only letters in the fields and make sure no
numbers have been entered. Any help is appreciated. Thanks in advance.
-D-
Jul 20 '05 #1
4 6125
Using regular expressions would be the easiest method:

/^[a-zA-z]+$/ only allow letters

<script type="text/validate">
function Validate()
{
// set pattern for text only
var textRE=/^[a-zA-z]+$/;

varTextObject1= document.forms['myform'].elements['mytextbox'];

// check contents of textbox 'mytextbox' in form 'myform' against pattern
if(!textRE.test (TextObject1.va lue)
{
alert('text field 1 can only accept text, not numbers or other
characters.');
TextObject1.foc us();
return false;
}
return true;
}
</script>

.......

<form name="myform" action="blap.ph p" method="get" onSubmit="retur n
Validate();">
<input type="text" name="mytextbox ">
<input type="submit" value="submit form">
</form>
for numbers only use /^[0-9]+$/
for mixed text and numbers, with spaces /^[0-9a-zA-Z\s]+$/

"Dwayne Epps" <dw************ **@centurytel.n et> wrote in message
news:5T******** ************@fe ed2.centurytel. net...
I've created a function that checks form fields that only will have letters. This is the script:

<script type="text/javascript" language="javas cript">
function validateString( field, msg, min, max) {
if (!min) { min = 1 }
if (!max) { max = 65535}
if (!field.value || field.value.len gth < min || field.value.max > max) {
alert(msg);
field.focus();
field.select();
return false;
}
return true;
}
</script>

How can I add code to this function that checks for whitespace...ex .
"\n\r\t"
I would also like to check for only letters in the fields and make sure no
numbers have been entered. Any help is appreciated. Thanks in advance.
-D-

Jul 20 '05 #2
"Dwayne Epps" <dw************ **@centurytel.n et> wrote in message news:<5T******* *************@f eed2.centurytel .net>...
I've created a function that checks form fields that only will have letters.
This is the script:

<script type="text/javascript" language="javas cript">
function validateString( field, msg, min, max) {
if (!min) { min = 1 }
if (!max) { max = 65535}
if (!field.value || field.value.len gth < min || field.value.max > max) {
alert(msg);
field.focus();
field.select();
return false;
}
return true;
}
</script>

How can I add code to this function that checks for whitespace...ex .
"\n\r\t"
I would also like to check for only letters in the fields and make sure no
numbers have been entered. Any help is appreciated. Thanks in advance.
-D-

Hi!
Well, did you know the regular expressions?!
Take a look!

funcion noNumbers( field ) {
if( field.value.mat ch( /\d/ ) {
alert('The string MUST not contain numbers!');
}
}

function removeSpaces( field ) {
field.value.rep lace( /\s/g, '' );
}

Here are some useful regular expressions:
[a-zA-Z] any letter
\d any number; same as [0-9]
\D any NOT number; same as [^0-9]
\w any alphanumeric character; same as [a-zA-Z-0-9_]
\W any NON-alphanumeric character; same as [^a-zA-Z0-9_]
\s any whitespace (tab, space, newline, etc...)
\S any NON-whitespace
\n newline
\t tab

Try search informations about regular expressions in JavaScript!

HTH
--
Edoardo
[http://edoardoontheweb.interfree.it/index.html]
Jul 20 '05 #3
Hi Eduardo,
Thanks for the replying to my post. I appreciate the help. I was
looking over the regular expressions as you suggested and got the check for
whitespace and numbers to work, but I am having difficulty validating
against irregular characters, i.e., !#$%^&*()+=?).

I'm using the function listed below to validate the form fields. So, if I
wanted to validate against irregular characters, what regular expression
could I use to check for the following irregular characters: !#$%^&*()+=?).
Thanks again for your help! I'm pretty new with Javascript, so I really
appreciate the help on this.

function noNumbers(field ) {
if (field.value.ma tch(/\d/)) {
alert('This field can not contain numbers!');
field.focus();
field.select();
return false;
}
return true;
}

-D-
"edoardo" <ed************ *@interfree.it> wrote in message
news:85******** *************** ***@posting.goo gle.com...
"Dwayne Epps" <dw************ **@centurytel.n et> wrote in message

news:<5T******* *************@f eed2.centurytel .net>...
I've created a function that checks form fields that only will have letters. This is the script:

<script type="text/javascript" language="javas cript">
function validateString( field, msg, min, max) {
if (!min) { min = 1 }
if (!max) { max = 65535}
if (!field.value || field.value.len gth < min || field.value.max > max) {
alert(msg);
field.focus();
field.select();
return false;
}
return true;
}
</script>

How can I add code to this function that checks for whitespace...ex .
"\n\r\t"
I would also like to check for only letters in the fields and make sure no numbers have been entered. Any help is appreciated. Thanks in advance.
-D-

Hi!
Well, did you know the regular expressions?!
Take a look!

funcion noNumbers( field ) {
if( field.value.mat ch( /\d/ ) {
alert('The string MUST not contain numbers!');
}
}

function removeSpaces( field ) {
field.value.rep lace( /\s/g, '' );
}

Here are some useful regular expressions:
[a-zA-Z] any letter
\d any number; same as [0-9]
\D any NOT number; same as [^0-9]
\w any alphanumeric character; same as [a-zA-Z-0-9_]
\W any NON-alphanumeric character; same as [^a-zA-Z0-9_]
\s any whitespace (tab, space, newline, etc...)
\S any NON-whitespace
\n newline
\t tab

Try search informations about regular expressions in JavaScript!

HTH
--
Edoardo
[http://edoardoontheweb.interfree.it/index.html]

Jul 20 '05 #4
"Dwayne Epps" <dw************ **@centurytel.n et> wrote in message news:<KD******* *************@f eed2.centurytel .net>...
Hi Eduardo,
Thanks for the replying to my post. I appreciate the help. I was
looking over the regular expressions as you suggested and got the check for
whitespace and numbers to work, but I am having difficulty validating
against irregular characters, i.e., !#$%^&*()+=?).

I'm using the function listed below to validate the form fields. So, if I
wanted to validate against irregular characters, what regular expression
could I use to check for the following irregular characters: !#$%^&*()+=?).
Thanks again for your help! I'm pretty new with Javascript, so I really
appreciate the help on this.

function noNumbers(field ) {
if (field.value.ma tch(/\d/)) {
alert('This field can not contain numbers!');
field.focus();
field.select();
return false;
}
return true;
}

-D-


Well, you can modify the function like this:

function noNumbers(field ) {
if( field.value.mat ch( /[!#$%^&*()+=?]/)\ ) ) {
alert('This field can not contain numbers!');
field.focus();
field.select();
return false;
}
return true;
}

The square brackets [] means that the matching returns 1 if at least a
character present between them is found.
You can add also \d if you want to match also numbers.

Bye
--
Edoardo
[http://edoardoontheweb.interfree.it/index.html]
Jul 20 '05 #5

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

Similar topics

5
2708
by: lawrence | last post by:
When users enter urls or other long strings it can destroy the formatting of a page. A long url, posted in a comment, can cause page distortions that make the page unreadable, till the website owner logs in and deletes the comment. To protect against that, I'd like to break up long strings in the comments (anything submitted by anonymous sources). One thing I'd like to add to the following function is the ability to break up long...
1
1783
by: Dan W. | last post by:
I've been acting as messenger the past few days between the BOOST and Digital Mars peoples, and they can't seem to come to an agreement about the semantics of using ## vs. juxtaposition. The problem arose when I was trying to compile a boost example program with the DM compiler, and the name of a file which was put together by a set of macros, ended up as, ....\...\list10 .cpp vs. ....\...\list10.cpp
12
1822
by: drM | last post by:
I have looked at the faq and queried the archives, but cannot seem to be able to get this to work. It's the usual factorial recursive function, but that is not the problem. It hangs after the user enters a number. However, as I indicate, if one adds something else after the number, the function proceeds and finishes successfully. I would appreciate some helpful hints. thanks in advance. >>>>>>>>>
14
1558
by: Peter | last post by:
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char input_string; printf("Please enter conversion: "); scanf("%s", input_string);
1
2215
by: siliconwafer | last post by:
Hi All, here is one code: int main() { FILE*fp; unsigned long a; fp = fopen("my_file.txt","w+"); a = 24; fprintf(fp,"%ld",a); while(fscanf(fp,"%ld",&a) == 1) {
3
441
by: Michael C | last post by:
Hi all, Quick question about the TreeView control. I'm using code like this to determine the currently clicked TreeNode in the TreeView. private void MyTreeView_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { currentNode = MyTreeView.GetNodeAt(e.X, e.Y); }
13
1454
by: Thomas Liesner | last post by:
Hi all, i am having a textfile which contains a single string with names. I want to split this string into its records an put them into a list. In "normal" cases i would do something like: > #!/usr/bin/python > inp = open("file") > data = inp.read() > names = data.split()
3
4191
by: pauldepstein | last post by:
The following description of atoi is pasted from cplusplus.com. My question is after the pasting. ***** PASTING BEGINS HERE ****** int atoi ( const char * str ); <cstdlib> Convert string to integer Parses the C string str interpreting its content as an integral
2
1221
by: kjmaclean | last post by:
Hello CSS guru's: I'm new to CSS and I'm having trouble deciphering markup like this: #navcontainer ul { margin: 0; } or even this: #navcontainer ul ul li { margin: 0; } In the first example, the id #navcontainer is followed by whitespace and another CSS element. In the second example, there are 3 elements that follow the id, which are also separated by whitespace. In the first example, I assume that the ul that is associated with...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9952
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8976
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7500
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3654
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.