Text box validation using regular expression/java script

Posted by Sreenuksr under ASP.NET on 10/22/2011 | Points: 10 | Views : 1989 | Status : [Member] | Replies : 2
I have a textbox and have to use the regular expressions in asp.net

My text should not allow the spaces in first and last place. In between words it can allow.
that means: it should allow alphabets, numbers and all special characters.

Output should be:

India Bangalore -valid
<space>India Bangalore<space> - not valid
!India bangalore - valid
India bangalore!@#$%- valid
India<space>Bangalore - valid

i.e : use can enter the spaces in between the words but not in first position and last position.

Java script also fine.

Thanks in advance.


Posted by: Blessyjees on: 10/25/2011 [Member] Bronze | Points: 25


try the regular expression ^[^ ][a-zA-Z0-9'\- ]+[^ ]$ to validate the textbox value.

Blessy Baby
Digitalmesh Softech pvt Ltd

Sreenuksr, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abdulhaknet on: 10/25/2011 [Member] Starter | Points: 25

hai srinu,

please go through the code below it allow alphabets, numbers and special charecters, and it will not allow space begin and end of the string

Please check it.

<SCRIPT LANGUAGE = "JavaScript1.2">
re = /^\s+|\s+$/g;
function testInfo(id) {
var str = document.getElementById(id).value;

Enter your phone number (with area code) and then press Enter.
Old Text
<INPUT TYPE="text" NAME="Phone" id="m1" onChange="testInfo(this.id);">
New Text
<INPUT TYPE="text" NAME="NPhone" id="m2" onChange="testInfo(this.id);">

Sreenuksr, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response