Text box validation using regular expression/java script

Posted by Sreenuksr under ASP.NET on 10/22/2011 | Points: 10 | Views : 2002 | 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.




Responses

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

Up
0
Down
hi,

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



Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

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

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

Up
0
Down
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.


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

</SCRIPT>
Enter your phone number (with area code) and then press Enter.
<FORM>
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);">
</FORM>
</HTML>

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

Login to post response