Click here to Skip to main content
15,915,869 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to validate 2 fields using compare validator
1)Email address (required field - used required field validator)
2)Confirm email address

If I enter abc@abc.com in email address AND abc@abc.com in confirm email address, it will work fine.

But when I add space after the email address in one of the textbox, it will say that both emails do not match which is not desirable. How to solve this ?

It would be helpful for the user if I would be able to notify to the user that the space is not allowed in email address.
Posted
Updated 24-Apr-13 4:37am
v2
Comments
[no name] 24-Apr-13 10:39am    
You would need to Trim the strings before comparing them.
Warreer 26-Apr-13 4:12am    
When do i trim the strings? I am also doin page.isvalid()on the Button click. I trimmed the strings on page load, it worked. But when I trim the strings on the that button,the strings get trimmed but validation doesn't work and shows the error message.
ZurdoDev 24-Apr-13 10:50am    
Personally, I would recommend you leave it as is. Somewhere you have to draw the line with how intelligent users are.
Warreer 24-Apr-13 10:55am    
actually yes, but my senior wont agree :D
ZurdoDev 24-Apr-13 11:07am    
Then you can't use the compare validator. You'll just have to write JS or jquery to compare them yourself. Unless, as solution 1 points out, the FilteredTextBoxExtender (which I have not used) works and then you won't allow spaces.

1 solution

You can use TrimEnd() method for the last space char:
C#
yourText = yourText.Trim(); //OR
yourText = yourText.TrimEnd();

OR
You can replace the spaces by Replace() method such as:
C#
Text.Replace(" ", string.empty);

But more importantly you should use FilteredTextBoxExtender for avoiding characters such as space and the other chars that you desire not be entered by user.

For more info check AJAX control tool kit's FilteredTextBoxExtender[^].

Use it on invalidchars.

Good luck,
OI
 
Share this answer
 
v2
Comments
Warreer 24-Apr-13 10:54am    
I just want to use simple asp.net compare validator

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900