4

Haven't been able to find a good answer to my situation yet. I want this textbox to only take numbers and still have the id "SearchString2" so I can use it in my controller. Any idea how?

if (Roles.IsUserInRole(WebSecurity.CurrentUserName, "Admin"))
{
    @:<p><b>Customer ID:</b> @Html.TextBox("SearchString2")</p>
}

Thanks in advance.

1

2 Answers 2

6

You can do something like this:

@Html.TextBox('SearchString2', new { @type = "number" }) 

This should set the type to be a number, you could then use attributes on your model to help limit it to only ints like so:

[RegularExpression(@"^[A-Z]+[a-zA-Z''-'\s]*$")]
[Required]
public string SearchString2 { get; set; }

You'll need to replace the regex with an actual regex and put an validation message in.

Here's more info on validation: http://www.asp.net/mvc/overview/getting-started/introduction/adding-validation

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

3 Comments

Thanks, was what I was looking for :)
Actually now that I try it...doesn't work at all with the first one. And I only use searchstring2 when an admin is logged in which would make it a bad idea to put it in the model.
What I do get is a textbox which is automatically filled out with "type = "number"
4

Actually, I think the correction needed to the above answer is

@Html.TextBox('SearchString2',null, new {@type="number"})

otherwise type=number shows up in the value.

Comments

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.