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.

5
  • You can use the onkeydown event in javascript. link Commented May 12, 2015 at 12:29
  • Then do it correctly and use a view model and add the [Required] attribute to the property of your view model. Commented May 12, 2015 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. Commented May 12, 2015 at 12:34
  • @Stephen : My model is in dll, and it does not validate from dll. Commented May 12, 2015 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? Commented May 12, 2015 at 12:41

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.
1

You can try this

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

Comments

1

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

2 Comments

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.
if(string.trim(yourTextBox.text) ==! "") { //Send data to server }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.