Rees
Rees

Reputation: 1787

how to prevent white spaces in a regular expression regex validation

I am completely new to regular expressions ,and I am trying to create a regular expression in flex for a validation.

Using a regular expression, I am going to validate that the user input does NOT contain any white-space and consists of only characters and digits... starting with digit.

so far i have:

expression="[A-Za-z][A-Za-z0-9]*"

This correctly checks for user input to start with a character followed by a possible digit, but this does not check if there is white space...(in my tests if user input has a space this input will pass through validation - this is not desired) can someone tell me how I can modify this expression to ensure that user input with whitespace is flagged as invalid?

Upvotes: 12

Views: 40521

Answers (3)

Thalaivar
Thalaivar

Reputation: 23632

"^[A-Za-z][A-Za-z0-9]*$"

http://ryanswanson.com/regexp/

In future go with the above link which will be very useful for you.

Upvotes: 1

Tim Pietzcker
Tim Pietzcker

Reputation: 336188

You'll need to anchor the regex at the start and end of the string:

expression="^[A-Za-z][A-Za-z0-9]*$"

makes sure that not just a substring but the entire string is matched by the regex.

Upvotes: 16

Anton
Anton

Reputation: 2693

Try "^[A-Za-z][A-Za-z0-9]*$".

Upvotes: 2

Related Questions