0

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.

Pranav
  • 85
  • 1
  • 2
  • 11
  • You can use the onkeydown event in javascript. [link](http://www.w3schools.com/jsref/event_onkeydown.asp) – Greg May 12 '15 at 12:29
  • Then do it correctly and use a view model and add the `[Required]` attribute to the property of your view model. –  May 12 '15 at 12:33
  • **RequiredAttribute** works well, as a validation exception is raised if the property is null, contains an empty string (""), or contains only white-space characters. – BabyDuck May 12 '15 at 12:34
  • @Stephen : My model is in dll, and it does not validate from dll. – Pranav May 12 '15 at 12:40
  • It does not matter where your data model is. Use a view model. Every view especially when editing should have a view model - [What is ViewModel in MVC?](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  May 12 '15 at 12:41

3 Answers3

1

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

phooey
  • 295
  • 2
  • 10
  • No.. I have model which sends value to the dll file via object. In this I cant edit each and every value and set again to the object of the model. – Pranav May 12 '15 at 12:33
1

You can try this

bool b= textBox1.Text.Length>0 && textBox1.Text.Trim().Length==0;
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

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

Community
  • 1
  • 1
  • I cant do that. As if I want to enter a value "Stack Overflow" the space in between stack and overflow will be replaced. Also I have already one javascript running that validates if the box is empty. – Pranav May 12 '15 at 12:38
  • 1
    `if(string.trim(yourTextBox.text) ==! "") { //Send data to server }` – Mohammad M. Ramezanpour May 12 '15 at 13:44