8

Hi i want to use below php regex in spry java script framework but them doesn't work with spry framework and spry doesn't let the user to input!.
1)"/^[\d]+$/"
2)"/^([\x{600}-\x{6FF}]+\s)*[\x{600}-\x{6FF}]+$/u"
3)"/^([\x{600}-\x{6FF}]+\d*\s)*[\x{600}-\x{6FF}]+\d*$/u"
please help me to convert them to use in spry framework.

david
  • 17,925
  • 4
  • 43
  • 57
fire boy
  • 163
  • 2
  • 11

2 Answers2

8
1) /^[\d]+$/
2) /^([\u0600-\u06FF]+\s)*[\u0600-\u06FF]+$/
3) /^([\u0600-\u06FF]+\d*\s)*[\u0600-\u06FF]+\d*$/

/u is not supported, since Javascript regexes only supports unicode in terms of codepoints. \x{???} (unicode codepoints) should be written \u???? in Javascript regex (always 4 digits 0 padded)

In these cases, the following applies to the rest of the regex:

  • \s in Javascript is treated as unicode
  • \d isn't, which means only ASCII digits (0-9) are accepted.

This means we specifically have to allow "foreign" numerals, e.g. Persian (codepoints 06F0-06F9):

1) /^[\d\u06F0-\u06F9]+$/
2) /^([\u0600-\u06FF]+\s)*[\u0600-\u06FF]+$/
3) /^([\u0600-\u06FF]+[\d\u06F0-\u06F9]*\s)*[\u0600-\u06FF]+[\d\u06F0-\u06F9]*$/

(Remove \d if ASCII digits shouldn't be accepted)

Not sure what the brackets are supposed to be doing in example 1, originally they could be written:

1) /^\d+$/

But to add the Persian numerals, we need them, see above.

Update

Spry character masking, however, only wants a regex to be applied on each entered character - i.e., we can't actually do pattern matching, it's just a "list" of accepted characters in all places, in which case:

1      ) /[\u06F0-\u06F9\d]/      // match 0-9 and Persian numerals
2 and 3) /[\u0600-\u06FF\d\s]/    // match all Persian characters (includes numerals), 0-9 and space

Once again, remove \d if you don't want to accept 0-9.

Update 2

Now... using regex for validation with Spry:

var checkFullName = function(value, options)
{
   // Match with the by now well-known regex:
   if (value.match(/^([\u0600-\u06FF]+\s)*[\u0600-\u06FF]+$/))
   {
      return true;
   }
   return false;
}

var sprytextfield =
     new Spry.Widget.ValidationTextField(
          "sprytextfield", 
          "custom", 
          { validation: checkFullName, validateOn: ["blur", "change"] }
     );

A similar custom function can be made for case 3.

See examples from Adobe labs

JimmiTh
  • 7,389
  • 3
  • 34
  • 50
  • Deleted previous comment and added some clarification on the unicode problems. @fireboy: Do you get an error, or does the regex block too much/too little? It may need Persian numerals, I'll edit again with an attempt at that. – JimmiTh Jan 30 '12 at 23:33
  • There was a typo in the 3rd one, but the 2nd should work, unless there's a special Persian space character that both Javascript and I don't know about. Basically it's a regex for "words separated by a single space". I'll look more into the space character. – JimmiTh Jan 30 '12 at 23:51
  • After that i type one persian character when i want to use space char i can't type it and i change my keyboard layout to english but i can't type space too!! in 2nd and 3nd regexes – fire boy Jan 30 '12 at 23:59
  • Or maybe it's because I missed the bit about using it as character masking in spry. As far as I know, character masking doesn't allow accepting different characters in different places, it's basically just a list of accepted characters expressed as a regex. See edited answer. – JimmiTh Jan 31 '12 at 00:35
  • Ok thanks for your help but in the above regex 2nd i want to user end the regex with persian chars not with space because when the user press space at end of full name in the server side php code php regex return error! but spry accept that! – fire boy Jan 31 '12 at 00:49
  • and another problem is /[\u0600-\u06FF\s]/ accept only space for full name! – fire boy Jan 31 '12 at 00:56
  • Well, that's the problem with "useCharacterMatching" - it doesn't care about *where* you type the characters. If you accept space in one spot, it accepts it in all spots. It's only meant to filter out characters the user is not allowed to type, not to validate the result. Out of the box, spry doesn't support client validation using regex at all, but I may add another answer on that. – JimmiTh Jan 31 '12 at 00:58
  • Thanks i use this regex /[\u0600-\u06FF\s?]/ but doesn't work and accept more space characters! – fire boy Jan 31 '12 at 01:10
  • Only because it's sent in by spry - it's not used in the function, though, so you can remove it if you want. – JimmiTh Jan 31 '12 at 01:32
  • Yeah, missed that - added a link to the examples I based it on in the answer. Adobe's docs tend to be notoriously lacking. Use view source to see the implementation of the examples. (Cleaning up some of the comments, to remove "extended discussions" as StackOverflow calls them). – JimmiTh Jan 31 '12 at 01:51
2

Are you passing them in as strings or as regex objects? Try removing the " characters from around the regex.

The 'u' flag is a little more tricky. You may need to explain what the 2nd and 3rd regexes are trying to do.

david
  • 17,925
  • 4
  • 43
  • 57
  • This is one of my regex 2d in spry framework. var sprytextfield1 = new Spry.Widget.ValidationTextField("spryAddress", "none", {validateOn:["blur"], maxChars:80, characterMasking:/^([\u0600-\u06FF]+\s)*[\u0600-\u06FF]+$/, useCharacterMasking:true}); – fire boy Jan 30 '12 at 23:11
  • Hmm okay, what are those characters meant to be? are you sure the conversion is from \x600 to \u0600? – david Jan 30 '12 at 23:14
  • /([\u0600-\u06FF])/ is persian unicode regex and i can use it in spry framework but i can't use above regex in spry framework.I don't know which problem it has!! – fire boy Jan 30 '12 at 23:20