WCF RIA Services DataAnnotations not working as expected - silverlight-5.0

I'm having a hard time understanding why my DataAnnotation attributes aren't be used by the client DataForm. Below is the metadata attribute on a phone number field.
[DataType(DataType.PhoneNumber, ErrorMessage = "Please provide a valid phone number.")]
public string client_phone_home { get; set; }
When running the application, I can enter any string less than 10 digits in length. It can be letters, numbers, etc. If I enter more than 10 characters, the datavalidation throws a message saying that the client_phone_home field must be a string less than 10 characters in length. It doesn't use my error message, and doesn't indicate anything about the field requiring a valid phone number.
Anyone know why this dataannotation is not working on the client side?

Check this http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx .It has complete explaination about Data Annotation Validation Attributes,Custom Validation Attributes,Server-Side Validation.
and look into this also http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2010/07/27/silverlight-and-wcf-ria-services-6-validation.aspx

Related

How to verify email address with regex and CSML

I would like to validate email address from user giving information to my chatbot.
Is there a simple way to deny unexpected symbols with regular expressions and CSML?
Here's my current code:
askEmail:
say "Thank you {{firstname}}"
say "What is your email address?"
hold
remember email = event
if (Find("#", in="email")) goto askCompany
else {
say "Please enter a real email address"
goto askEmail
}
I need to replace Find with a function using regexp and check all unexpected symbols.
There is a contains_regex string method in CSML that you can use to verify more complex rules than just "is there a # in this string?".
It's quite hard to cover all possible valid email addresses with a regex (see https://emailregex.com), but let's just agree for your purpose that emails are in the general form of "whatever#whatever.whatever" where "whatever" is any string that does not contain any line break.
This is obviously not a 100% foolproof regex as it is a bit naive, but validating emails is still a Hard Thing ®. Good enough is usually good enough.
Here goes:
askEmail:
say "Thank you {{firstname}}"
say "What is your email address?"
hold
remember email = event
if (email.contains_regex("^.+#.+\..+$")) goto askCompany
else {
say "Please enter a real email address"
goto askEmail
}
You can obviously tweak the regex to your liking!
emailPattern = /^([a-z\d]{1})([\w-\.!#$%&'*+\/=?\^`\{\|\}~"\(\),:;<>\[\\\]]){2,}([a-z\d]{1})#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-z\-0-9]+\.)+[a-z]{2,}))$/i;
console.log('Regex test is: ', emailPattern.test('test#test.test'));
emailPattern = /^([a-z\d]{1})([\w-\.!#$%&'*+\/=?\^`\{\|\}~"\(\),:;<>\[\\\]]){2,}([a-z\d]{1})#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-z\-0-9]+\.)+[a-z]{2,}))$/i;
Email Regular Expression Pattern
^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)* #[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$;

Custom Regex validation firing on all inputs

I'm writing a .NET MVC application and using unobtrusive validation to sanitize my client inputs based on data annotations in my model. I have an input that I do not want to allow HTML tags into and would like to display a custom error message if an html tag is entered. As such I have created a data annotation with a custom regex expression to cover these conditions, like so:
[Required(ErrorMessage = "You must provide a First Name.")]
[RegularExpression(#"<[a-z][\s\S]*>", ErrorMessage = "Invalid character")]
[DisplayName("First Name")]
public string FirstName { get; set; }
The issue with this is, no matter what character, whether it be <test> or whether it be abc will cause the Invalid Character message to appear. The required attribute works fine, and if I try a simple regex such as:
[RegularExpression("[a-z]", ErrorMessage = "Invalid character")]
This works 100% as expected, leading me to believe my regex is incorrect, nut I know it works for HTML validation as I can prove it out with online tools. What am I doing wrong?
If you take a look at the documentation of the RegularExpressionAttribute, it states:
Specifies that a data field value in ASP.NET Dynamic Data must match the specified regular expression.
So your attribute is doing the exact opposite of what you want to do is:
[RegularExpression(#"^(?!.*<.*>).*$", ErrorMessage = "Invalid character")]

How to validate input text for integers and decimals only?

I cannot find the right answer for my case, so I posted my question here.
I'm validating the form in ASP.NET MVC and looking for the way to validate a text field to allow only numeric and decimal numbers like
1 or 1.5 or 1.65
If I have 1,65 I do not want this to be validated.
I have put a metadata on my Model's field like this: [RegularExpression(#"^(((\d{1})*))$")]
And have
#Html.ValidationMessageFor(m => m.ResolvedAmount, "", new { #class = "error" })
to validate the field.
Also, in my function I have the following to check the validation:
var validator = $("#main").kendoValidator().data('kendoValidator');
if(validator.validate()){some logic}
I validate for the required field, but cannot get my int/decimal validation working.
What do I need to have in order to validate it?

RegularExpressionAttribute fails validating right data

I have a regular expression that works great when I try it:
System.Text.RegularExpressions.Regex.IsMatch("universal",#"^[A-Za-z0-9 ._’&-/s]{0,100}$")
true
System.Text.RegularExpressions.Regex.IsMatch("universal £$%$£%",#"^[A-Za-z0-9 ._’&-/s]{0,100}$")
false
But when I use it as a validation filter:
[RegularExpression(#"^[A-Za-z0-9 ._’&-/s]{0,100}$", ErrorMessage = "The parameter is not valid")]
It works in the client side, but it does not work on the server side. For example when I pass the word "universal" the ModelState contains an error regarding the field marked with that regex validator.
This attribute is the only validation rule applied to that field, what may be the problem?
Cheers.

Using Fluent NHibernate Persistence Tester Tool with Object that has encrypted Password field

First of all: I am completely new to Fluent and NHibernate.
I have a User object that contains a password field. When setting that field the value gets encrypted. Now I try to use the
new PersistenceSpecification<User>(session)
...
.CheckProperty(p => p.Password, "secret")
...
.VerifyTheMappings();
persistence checker tool. The problem I am facing is the handling of the password field. The debugger told me that the tool calls the Password field setter multiple times. The first time with the cleartext password "secret". The following times with the encrypted versions ending up with encrypting my password multiple times.
Any idea how to cope with this?
EDIT:
NHibernate has to set the password on the user after it loaded it from db and when you use the standard
Map(user => user.Password);
it will use the property to set the db-value
Map(user => user.Password).Access.CamelCaseField(Prefix.Underscore);
prevents this then
A way to solve this is to use a setter method to set your password, for example
public virtual void UpdatePassword(string newPassword)
{
string hashedPassword = HashPassword(newPassword);
_password = hashedPassword;
}
This way your hash/encryption logic is separate from your property setter. In your PersistenceSpecification test you will then be testing the saving/getting of the encrypted text.
If you need to decrypt the password use a getter method to get the decrypted password (the Password property will contain the encrypted text).
public virtual string GetPasswordDecrypted()
{
...
}
As a side note, unless you have a good reason it will be better to hash the password than encrypt it.