Pranav
Pranav

Reputation: 95

Restrict Users from entering only spaces in textbox in c# mvc

I have a view where in textbox user enters a value. It may be number, character, special character anything. But I want to validate that user is not allowed to enter ONLY spaces. User can enter spaces with characters, but ONLY spaces are not allowed.

For eg.

User can enter Name : Stack ""space" Overflow

but user should not be allowed to enter

Name : "space" "space" "space"

Problem is I cant check it server side as my models are DTO defined in another project which is loaded as dll in this one.

Upvotes: 0

Views: 1995

Answers (3)

Define a trim function in Javascript. More information can be found here: Trim string in JavaScript?

Upvotes: 1

Rahul Tripathi
Rahul Tripathi

Reputation: 172418

You can try this

bool b= textBox1.Text.Length>0 && textBox1.Text.Trim().Length==0;

Upvotes: 1

phooey
phooey

Reputation: 295

Could you not trim the text and check if the length is greater than zero?

Upvotes: 1

Related Questions