Question

I have a property on a model that accepts a string value and stores hex values for colors

eg: EEEEEE, 000000

I integrated a jquery plugin so that the user can use a color picker. I like it since it's very easy to use. The problem is that when you pick a color it fills up the text box with a "#" character which then makes it easy to change the color of the textbox (for User friendliness purposes - it's part of the plugin).

Now I want to save the value on post but the problem is the validation on the model will only accept the following characters "0-9A-F". Which I also want to maintain (no special characters on the database).

So I figured to avoid the validation I would modify the textbox on click of the button. My questions are:

1. Is that the most efficient way?

2. If it is, how would i do it using jquery? or something else?

Let me know your thoughts.

Thanks!!!

Was it helpful?

Solution

I would use a regular expression validator as follows on your model:

[RegularExpression(@"#([0-9][A-F])*", ErrorMessage = "Invalid Color Code")]

This will include the "#" in the validation, so it will pass validation, then you can deal with the "#" on the server side, if you need to.

If you can't change the model validation, maybe you could do something in JavaScript when the before the form is submitted to remove the "#" from the value that is submitted.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top