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

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="javascript">
function validateString(field, msg, min, max) {
if (!min) { min = 1 }
if (!max) { max = 65535}
if (!field.value || field.value.length < 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 6098
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.value)
{
alert('text field 1 can only accept text, not numbers or other
characters.');
TextObject1.focus();
return false;
}
return true;
}
</script>

.......

<form name="myform" action="blap.php" method="get" onSubmit="return
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.net> wrote in message
news:5T********************@feed2.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="javascript">
function validateString(field, msg, min, max) {
if (!min) { min = 1 }
if (!max) { max = 65535}
if (!field.value || field.value.length < 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.net> wrote in message news:<5T********************@feed2.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="javascript">
function validateString(field, msg, min, max) {
if (!min) { min = 1 }
if (!max) { max = 65535}
if (!field.value || field.value.length < 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.match( /\d/ ) {
alert('The string MUST not contain numbers!');
}
}

function removeSpaces( field ) {
field.value.replace( /\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.match(/\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.google.c om...
"Dwayne Epps" <dw**************@centurytel.net> wrote in message

news:<5T********************@feed2.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="javascript">
function validateString(field, msg, min, max) {
if (!min) { min = 1 }
if (!max) { max = 65535}
if (!field.value || field.value.length < 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.match( /\d/ ) {
alert('The string MUST not contain numbers!');
}
}

function removeSpaces( field ) {
field.value.replace( /\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.net> wrote in message news:<KD********************@feed2.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.match(/\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.match( /[!#$%^&*()+=?]/)\ ) ) {
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
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...
1
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...
12
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...
14
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
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
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,...
13
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: >...
3
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...
2
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.