I have two fields, one that not allow save HTML tags on DB and the other allow.
How I create an validator that remove spetial chars and tags from my string before save, and apply on String fields only?
PS.: I need something to block SQL inject too.
Actually validator cannot remove any chars. Validator just checks whether entered content is valid or not.
Read for example
For your case you can define form fields and mar them with
#Pattern(regexp = "the detect html expression here")
See regular expressions to [detect][2] (and remove) html
Related
I have created a #url constraint validation and provided regex. Above it I have annotated #pattern. So, whenever in my bean I am entering value in field url, the url is getting validated
Currently i have a regex which is validating this url. "http://www.google.com".
My new requirement is it should validate only "www.google.com"
I can just change the regex and it will work. but the problem is there are 2 different scenarios
1.In one i need to validate "http://www.google.com"
2.In second i need to validate this "www.google.com"
So, i cannot change the regex. Can anyone please help how can I resolve this? If i need to use custom validator instead of hibernate validator, then how can i achieve this?
I have tried to create
1.custom validator and in isvalid method i am validating the url without #Pattern
2.I have tried #Pattern by changing my regex to just validate domain and path and skip protocol http
I have a text box on my form. I want only a comma separated keyword in that. How to put this type of validation in symfony2.
I found
http://symfony.com/doc/current/reference/constraints.html
and
http://symfony.com/legacy/doc/forms/1_2/en/02-form-validation
but not as I required.
well the validation logic is as simple as
if(is_array(explode(",",$input)){
return true;
}else{
return false;
}
use a custom validator and implement this logic
You should write your custom validator for check this requirements, then you can implements a data transformer so you can manipulate your string as you want. As example, if you are archiving something like a tagging field you can proceed as described.
In the client side you can add a js component that can do this behaviour for you, check this component in the Tagging support section (you can build a REST service for manage a subset of filed).
Let me know if this is your scenario so i can provide example code about.
Hope this help.
I am working on struts server side validation(version 1.2). I have used validwhen several times. I have 2 textboxes that submit to server. text1(value comes here from a drop down selection) can have name or id. text 2 holds its value. I have to write validation on text2. Is there a way where i can change mask var-value in validwhen ? i.e if text1 value is name then text2's mask should be a regex for name(alphanumeric and space). if text1 value is id then text2's mask should be a regex for id(only numbers). Is there a way this can be achieved? I don't think we can write 2 different validation on same field.
thanks,
IMO validation like that belongs on the Java side.
You can either wrap it up in a custom validator if it will be used across the app, or as part of the form's validation method, where you could also combine it with standard XML validation.
I use the validation attribute on the fields of a class and has a requirement like that:
if field 'a' is validate succeed then process the validation of field 'b' but if the field 'a' is not throw validation , ignore the validation of field 'b'.
does it feasible or i should think in other way?
public class myclass
{
[required]
public string a{get;set;}
[required]
public string b{get;set;]
}
i want:
1.if a pass the validate, then execute b 's validate
2.if a not pass the validate , then don't execute the b 's validate
Check out the MVC.ValidationToolkit
http://blogs.msdn.com/b/simonince/archive/2011/09/29/mvc-validationtookit-alpha-release-conditional-validation-with-mvc-3.aspx
It contains these two validations that will help you out.
RequiredIfAttribute. This attribute says “this field is required if some other field has value X”. It is used as [RequiredIf(“OtherField”, “TargetValue”)]
RequiredEmptyIfAttribute. This attribute says “this field must be empty if some other field has value X”. It is used as [RequiredEmptyIf(“OtherField”, “TargetValue”)]
Implement the IValidateableObject interface in your model to contain your custom validation logic. Since you cannot apply validation checks in the manner you've requested you need to resort to your own custom validation. I don't think Michael's suggestion )(although a good one) will work if you really need to say 'only if that is valid, validate something else' through attributes but this is done easily through this interface. However note this will be server side validation only unless you want to write your own client portion for this as well.
I'm trying to add the jQuery Validation plugin to some websites and I'm running into a bit of an issue in that the fields that it's supposed to validate have a prefix on the name property, such as "Customer.FirstName".
Since you have to pass a JSON object to the validate function for the rules, it doesn't work because it never finds the elements.
Is there a way to do per field basis, or can I still pass in a variant of the JSON object that specifies the field id as a string, such as "#Customer\.FirstName"?
Thanks in advance!
EDIT:
Per Greg's suggestions, I got it to work. So for anyone who has issues like these, you have to do it like this:
$("form").validate({
rules: {
"Prefix.FieldName": "validationKeyword"
}
});
DO NOT add the "#" to the selector, and DO NOT add the "\\" escape chars to the selector. So, "#Prefix\\.FieldName" will not match anything, so just leave those chars out.
JSON supports keys with "." in them - just quote them:
var obj = {"#Customer.FirstName": "value"};
In fact to be proper JSON they should always be double-quoted.
Edit: if this is a selector then you can escape the . like this: "#Customer\\.FirstName"
Edit2: The docs say the key needs to be a name so I it should either be "Customer.Firstname" or "Customer\.Firstname" depending on how well-coded the plugin is. You'll need <input name="Customer.Firstname" ...>.