You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Carlos Cajina <ce...@hotmail.com> on 2005/02/01 00:38:09 UTC

[UNICODE characters in Mask validation rule]

Good evening. I'm a little stuck trying to create a global constant in validation.xml that allows the mask rule to validate "special" spanish characters like Ñ [ UNICODE =  \u00d1 ] for names, surnames, city names and so... Can someone give a hint ;^) ? Mi RegExp is like this: ^[A-Z.Ñ\s]*$

Regards,

Carlos

Re: [UNICODE characters in Mask validation rule]

Posted by Kishore Senji <ks...@gmail.com>.
Here is the test case

import java.io.*;
import java.util.*;
import org.apache.commons.validator.*;

public class TestValidatorMask{
  public static void main(String[] args)
  throws Exception {
    ValidatorResources vr = new ValidatorResources(getFormSetAsInputStream());
    Form form = vr.getForm(Locale.US, "testForm");
    Field field = form.getField("firstName");
    List list = Arrays.asList(new String[]{"AA.Ñ  ", ".C.ÑÑÑÑ  ", "  
... ABC ZÑÑ.. ", "  @gÑÑÑ..Ñ."});
    for(Iterator iterator=list.iterator(); iterator.hasNext();){
		String value = (String)iterator.next();
		System.out.println("value = "+value+" mask =
"+field.getVarValue("mask")+" mapassed test? == "+validateMask(value,
field.getVarValue("mask")));
	}
  }

  public static boolean validateMask(String value, String mask){
	  return GenericValidator.matchRegexp(value, mask);
  }

  public static InputStream getFormSetAsInputStream(){
	  StringBuffer sb = new StringBuffer();
	  sb.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>")
        .append("<!DOCTYPE form-validation PUBLIC ")
        .append("\"-//Apache Software Foundation//DTD Commons
Validator Rules Configuration 1.0//EN\" ")
        .append("\"http://jakarta.apache.org/commons/dtds/validator_1_0.dtd\">")
	    .append("<form-validation><global><constant><constant-name>zip</constant-name>")
        .append("<constant-value>^[A-Z.&#209;\\s]*$</constant-value>")
        .append("</constant></global><formset><form
name=\"testForm\"><field    property=\"firstName\" depends=\"mask\">")
        .append("<arg0 key=\"prompt.name\"/><var>")
		.append("<var-name>mask</var-name>")
		.append("<var-value>${zip}</var-value>")
	    .append("</var></field></form></formset></form-validation>");
        return new ByteArrayInputStream(sb.toString().getBytes());
  }

}


On Mon, 31 Jan 2005 23:09:42 -0600, Carlos Cajina - Hotmail
<ce...@hotmail.com> wrote:
> I just couldn't resist :^P  ... Tomorrow I'll try it, but for now this looks
> like a "simpler" solution to the character encoding problem:
> 
> http://www.servletsuite.com/servlets/encflt.htm
> 

It should be very simple to roll your own filter. The main reason to
write a filter is to set the encoding before any reads from the input
stream. You Just have to implement Filter and in the doFilter method
set the character encoding on the request by calling
request.setCharacterEncoding(string). You can have the encoding
initialized as a init parameter or however you want.

for more info on writing filters take a look at
http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Servlets8.html#72440

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [UNICODE characters in Mask validation rule]

Posted by Carlos Cajina - Hotmail <ce...@hotmail.com>.
I just couldn't resist :^P  ... Tomorrow I'll try it, but for now this looks 
like a "simpler" solution to the character encoding problem:

http://www.servletsuite.com/servlets/encflt.htm



----- Original Message ----- 
From: "Kishore Senji" <ks...@gmail.com>
To: "Struts Users Mailing List" <us...@struts.apache.org>
Sent: Monday, January 31, 2005 7:47 PM
Subject: Re: [UNICODE characters in Mask validation rule]


> Good evening. I'm a little stuck trying to create a global constant in 
> validation.xml that allows the mask rule to validate "special" spanish 
> characters like Ñ [ UNICODE =  \u00d1 ] for names, surnames, city names 
> and so... Can someone give a hint ;^) ? Mi RegExp is like this: 
> ^[A-Z.Ñ\s]*$

 Something like this, I suppose

 <global>
    <constant>
      <constant-name>name</constant-name>
      <constant-value> ^[A-Z.\u00d1\s]*$</constant-value>
  </global>


But make sure you use the right character encoding for the request.
http://marc.theaimsgroup.com/?l=struts-user&m=110484806414499&w=2
should give your more information on character encoding

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [UNICODE characters in Mask validation rule]

Posted by Kishore Senji <ks...@gmail.com>.
> Hi Kishore, thanks for the tip... I tried what you suggested but it didn't
> work... while googling for answers I read somewhere that including UNICODE
> codes was only possible in *.properties files, not in *.XML ... from my
> experience, that seems to be right...

You are right. You have to declare unicode characters using character
entities in xml.

For this example it would be

<global>
   <constant>
     <constant-name>name</constant-name>
     <constant-value> ^[A-Z.&#209;\s]*$</constant-value>
 </global>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [UNICODE characters in Mask validation rule]

Posted by Carlos Cajina - Hotmail <ce...@hotmail.com>.
Hi Kishore, thanks for the tip... I tried what you suggested but it didn't 
work... while googling for answers I read somewhere that including UNICODE 
codes was only possible in *.properties files, not in *.XML ... from my 
experience, that seems to be right...

Anyway, I'm going through the thread you pointed... So far it seems the 
Character Encoding Filter is a good solution, but I keep hoping that some 
"simpler" solution is around the corner :^)

Thanks again Kishore... Have a good night!

Regards,

Carlos


----- Original Message ----- 
From: "Kishore Senji" <ks...@gmail.com>
To: "Struts Users Mailing List" <us...@struts.apache.org>
Sent: Monday, January 31, 2005 7:47 PM
Subject: Re: [UNICODE characters in Mask validation rule]


> Good evening. I'm a little stuck trying to create a global constant in 
> validation.xml that allows the mask rule to validate "special" spanish 
> characters like Ñ [ UNICODE =  \u00d1 ] for names, surnames, city names 
> and so... Can someone give a hint ;^) ? Mi RegExp is like this: 
> ^[A-Z.Ñ\s]*$

 Something like this, I suppose

 <global>
    <constant>
      <constant-name>name</constant-name>
      <constant-value> ^[A-Z.\u00d1\s]*$</constant-value>
  </global>


But make sure you use the right character encoding for the request.
http://marc.theaimsgroup.com/?l=struts-user&m=110484806414499&w=2
should give your more information on character encoding

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [UNICODE characters in Mask validation rule]

Posted by Kishore Senji <ks...@gmail.com>.
> Good evening. I'm a little stuck trying to create a global constant in validation.xml that allows the mask rule to validate "special" spanish characters like Ñ [ UNICODE =  \u00d1 ] for names, surnames, city names and so... Can someone give a hint ;^) ? Mi RegExp is like this: ^[A-Z.Ñ\s]*$

 Something like this, I suppose
 
 <global>
    <constant>
      <constant-name>name</constant-name>
      <constant-value> ^[A-Z.\u00d1\s]*$</constant-value>
  </global>


But make sure you use the right character encoding for the request. 
http://marc.theaimsgroup.com/?l=struts-user&m=110484806414499&w=2
should give your more information on character encoding

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org