Problem with Regoular Expression

M

M. Magistri

Hi everyone!

I need help with ER Expressions in JavaScript. I would like to know how to
match text in a textarea object.

I use the ollowing ER but it doesn't funtion:

/^[a-zA-Z]([a-zA-Z0-9\.\-\'\ \n\!]){2,500}/

I also tried to use '$' and 'gm' commands but it doesn't function.

What do i have to do?

Thank You!

Massimiliano
 
A

Andy Dingley

I need help with ER Expressions in JavaScript. I would like to know how to
match text in a textarea object.

What is your problem ?

- You have some text, you have an idea for a pattern, you need to
write the regex pattern ? What pattern are you looking for ?

- You don't know how to use String.match() etc. from JavaScript ?

- You're trying to access the contents of a <textarea>, but don;t
know how to write the DHTML ?

I use the ollowing ER but it doesn't funtion:
/^[a-zA-Z]([a-zA-Z0-9\.\-\'\ \n\!]){2,500}/

Of course it functions. It just isn't doing what you expected.
 
T

Toby A Inkster

M. Magistri said:
I use the ollowing ER but it doesn't funtion:
/^[a-zA-Z]([a-zA-Z0-9\.\-\'\ \n\!]){2,500}/

First answer these:

What do you *want* it to do?

What does it do now that differs from what you want?

Unless you answer those questions, then there's not much we can do.

But at a guess, get rid of the parentheses (I don't think they're needed)
and add in '\r' somewhere near '\n'.
 
M

M. Magistri

I'm sorry...I didn't explain clearly my problem....

Well,

I've programmed a javascript .js file where i control the content of a
TEXTAREA object before sending it to an ASP file.

So i want to send to ASP file only texts wich ara generated from the
following SET of chars {a-z, A-Z, 0-9, \n, !, (space), -, ., , (comma), '}.

The prevoius expression ( /^[a-zA-Z]([a-zA-Z0-9\.\-\'\ \n\!]){2,500}/ ) I
sent doesn't do what i *want* it has to do!

For example i want to accept the following multiline text:

"
Hi Everyone!

I'm a 'novice'!
"

But i don't want to accept this:

"
Hi everyonè!

I'm a "novice_" (I don't accept ' " ', ' è' and '_' chars)

"

Thank you for your quickly help! :))

Massimiliano, from Rome.
Andy Dingley said:
I need help with ER Expressions in JavaScript. I would like to know how to
match text in a textarea object.

What is your problem ?

- You have some text, you have an idea for a pattern, you need to
write the regex pattern ? What pattern are you looking for ?

- You don't know how to use String.match() etc. from JavaScript ?

- You're trying to access the contents of a <textarea>, but don;t
know how to write the DHTML ?

I use the ollowing ER but it doesn't funtion:
/^[a-zA-Z]([a-zA-Z0-9\.\-\'\ \n\!]){2,500}/

Of course it functions. It just isn't doing what you expected.
 
C

Chris Morris

M. Magistri said:
I've programmed a javascript .js file where i control the content of a
TEXTAREA object before sending it to an ASP file.

So i want to send to ASP file only texts wich ara generated from the
following SET of chars {a-z, A-Z, 0-9, \n, !, (space), -, ., , (comma), '}.

Just as a matter of interest, what would happen if the ASP file *did*
get text outside that list? I assume it checks *as well* that the
input is valid and rejects if not, or you're going to have problems
later.
The prevoius expression ( /^[a-zA-Z]([a-zA-Z0-9\.\-\'\ \n\!]){2,500}/ ) I
sent doesn't do what i *want* it has to do!

/^[a-zA-Z0-9\n! .,'-]+$/

As said elsewhere, you probably want \r\n rather than \n as well. And
you might need to use \' rather than ' but I'm not familiar enough
with JS to say.
 
A

Andrew Urquhart

*Chris Morris* said:
*M. Magistri* said:
The prevoius expression ( /^[a-zA-Z]([a-zA-Z0-9\.\-\'\
\n\!]){2,500}/ ) I sent doesn't do what i *want* it has to do!

/^[a-zA-Z0-9\n! .,'-]+$/

As said elsewhere, you probably want \r\n rather than \n as well. And
you might need to use \' rather than ' but I'm not familiar enough
with JS to say.

/^[a-z0-9\r\n\f\s\.,'!-]{2,500}$/i

E.g.
var strFormFieldValue =
document.forms["myForm"].elements["myField"].value;
if ((/^[a-z0-9\r\n\f\s\.,'!-]{2,500}$/i).test(strFormFieldValue)) {
// Yes, valid data
}
else {
// No, invalid data
}
--
Andrew Urquhart
- FAQ: www.html-faq.com/
- Archive: www.google.com/groups?q=alt.html
- Contact: www.andrewu.co.uk/contact/
- Employ me: Front/middle tier ASP developer with WAI & web standards
 
K

KirstyH

M. Magistri said:
I use the ollowing ER but it doesn't funtion:
/^[a-zA-Z]([a-zA-Z0-9\.\-\'\ \n\!]){2,500}/

Firstly I know nothing about Javascript and REs and am basing the
following on my, somewhat old now, experience with sed ;-)

I think you are matching any line starting with a letter followed by
2-500 of a-zA-Z0-9.-' n! (where I've removed the \ for clarity \n is not
EOL ($ is) unless js allows you to use it as such). You are NOT
matching double quote so you will never match

"
Hi Everyone!

I'm a 'novice'!
"

but you will match

I'm a 'novice'!

I think you would need something like

(^[a-zA-Z0-9\.\-\'\"\ \!]$)*

where the * could be {2-500} if you want between 2 and 500 characters.

Kirsty
 
C

Chris Morris

KirstyH said:
M. Magistri said:
I use the ollowing ER but it doesn't funtion:
/^[a-zA-Z]([a-zA-Z0-9\.\-\'\ \n\!]){2,500}/

Firstly I know nothing about Javascript and REs and am basing the
following on my, somewhat old now, experience with sed ;-)

I think you are matching any line starting with a letter followed by
2-500 of a-zA-Z0-9.-' n! (where I've removed the \ for clarity \n is
not EOL ($ is) unless js allows you to use it as such). You are NOT
matching double quote so you will never match

\n isn't being used as EOL in this case, though. Still, the OP will
need to enable the option for multiple-line regular expressions,
assuming JS lets you. If it doesn't, then there's no real way to do
this in JS other than running the regex over each line of the input in
turn.
I think you would need something like

(^[a-zA-Z0-9\.\-\'\"\ \!]$)*

where the * could be {2-500} if you want between 2 and 500 characters.

Wouldn't that only match single-character lines?
 
K

KirstyH

Chris said:
I think you would need something like

(^[a-zA-Z0-9\.\-\'\"\ \!]$)*

where the * could be {2-500} if you want between 2 and 500 characters.


Wouldn't that only match single-character lines?

Yes. I'm an idiot. It's missing a * between the ] and the $.

^[a-zA-Z0-9\.\-\'\"\ \!]*$)*
needs extra * here ^

The first * matches 0 or more characters (selected from the []
expression), the second 0 or more matching occurances of the thing in
parentheses (in this case lines).

Kirsty
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top