Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
Locked
0

RegExp for excluding special characters in a string.

New Here ,
Apr 21, 2012 Apr 21, 2012

Hi All,

Im using Flex RegExpValidator. Can anyone suggest me the correct expression to validate this condition?....

I have tried this expression :----- /^[^///\/</>/?/*&]+$/...But in this it is also negating the alphabets.Also I have tried with opposite condition that in the String we should have alphabets and the expression is:-- ([a-z]|[A-Z]|[0-9]|[ ]|[-]|[_])*..... Please can anyone help me on this.

Thanks in advanced to all.

Munira

26.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 23, 2012 Apr 23, 2012
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 24, 2012 Apr 24, 2012

Hi ,

Thanks for your reply.I have tried this condition.

And got following result:--

If you give only alphabets with no special symbols.The expression is valid

that is what is my requiremnet.

If you give only special characters .Expression is invalid what is our

requiremnet.

But if you give mixed expression of Special symbols and alphanumeric

characters it is considering it as Valid .

iwr ote:

**

Re: RegExp for excluding special characters in a string. created by

spoboyle <http://forums.adobe.com/people/spoboyle> in Flex - View the

full discussion <http://forums.adobe.com/message/4354492#4354492

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 24, 2012 Apr 24, 2012

did you use

var pattern:RegExp = /^[a-zA-Z0-9 -_]*$/;

or

var pattern:RegExp = /^[a-zA-Z0-9 \-_]*$/;

remember - needs to be escaped since it's used to indicate a range of values

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 24, 2012 Apr 24, 2012

I have used these expressions ,but nothing worked properly with

RegExpValidator:--

/[///\/</>/?/*&]+$/

^[a-zA-Z0-9_-]$

^[-_,A-Za-z0-9]$

Also I have tried with the expressions u have provided .But the same

problem occurs it Invalidates alphanumeric characters as well.

May be its RegExpValidator issue or some expression issue.

What I have noticed is that the negate character (^) is not considered as

negate.

Please assist me to get it resolved.

Thanks and Regards

Munira

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 25, 2012 Apr 25, 2012

^ at the start is not a negate its a start of line character

^ within a [] is a negation

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 25, 2012 Apr 25, 2012

Yes you are correct ,but I have tried this with single special character

say

Then also its not negating it ...

Can you please put some light on it.

Thanks and Regards

Munira

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 26, 2012 Apr 26, 2012

sorry but you are posting things back that do not make any sense

what do you mean with the below comment?

munira06 wrote:

Yes you are correct ,but I have tried this with single special character

say

Re: RegExp for excluding special characters in a string.

here is a sample app taken from the live docs

using ^[a-zA-Z0-9 \-_]*$ as the regex accepts all characters from a-z, A-Z, 0-9 - [space] and_

run the example tell me what regex you are using and what test strings fail when they should pass or pass when they should fail

<?xml version="1.0" encoding="utf-8"?>

<!-- Simple example to demonstrate the RegExpValidator. -->

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

        xmlns:s="library://ns.adobe.com/flex/spark"

        xmlns:mx="library://ns.adobe.com/flex/mx">

    <fx:Script>

        <![CDATA[

            import mx.events.ValidationResultEvent;

            import mx.validators.*;

            // Write the results to the

            private function handleResult(eventObj:ValidationResultEvent):void {

                if (eventObj.type == ValidationResultEvent.VALID) {

                    // For valid events, the results Array contains

                    // RegExpValidationResult objects.

                    var xResult:RegExpValidationResult;

                    reResults.text = "";

                    for (var i:uint = 0; i < eventObj.results.length; i++) {

                        xResult = eventObj.results;

                        reResults.text=reResults.text + xResult.matchedIndex + " " + xResult.matchedString + "\n";

                    }

                } else {

                    reResults.text = "";

                }

            }

        ]]>

    </fx:Script>

    <fx:Declarations>

        <mx:RegExpValidator id="regExpV"

                source="{regex_text}" property="text"

                flags="g" expression="{regex.text}"

                valid="handleResult(event)"

                invalid="handleResult(event)"

                trigger="{myButton}"

                triggerEvent="click"/>

    </fx:Declarations>

    <s:Panel title="RegExpValidator Example"

            width="75%" height="75%"

            horizontalCenter="0" verticalCenter="0">

        <s:VGroup left="10" right="10" top="10" bottom="10">

            <s:Label width="100%" text="Instructions:"/>

            <s:Label width="100%" text="1. Enter text to search. By default, enter  a string containing the letters ABC in sequence followed by any digit."/>

            <s:Label width="100%" text="2. Enter the regular expression. By default, enter ABC\d."/>

            <s:Label width="100%" text="3. Click the Button control to trigger the validation."/>

            <s:Label width="100%" text="4. The results show the index in the text where the matching pattern begins, and the matching pattern. "/>

            <mx:Form>

                <mx:FormItem label="Enter text:">

                    <s:TextInput id="regex_text" text="xxxxABC4xxx" width="100%"/>

                </mx:FormItem>

                <mx:FormItem label="Enter regular expression:">

                    <s:TextInput id="regex" text="ABC\d" width="100%"/>

                </mx:FormItem>

                <mx:FormItem label="Results:">

                    <s:TextInput id="reResults" width="100%"/>

                </mx:FormItem>

                <mx:FormItem >

                    <s:Button id="myButton" label="Validate"/>

                </mx:FormItem>

            </mx:Form>

        </s:VGroup>

    </s:Panel>

</s:Application>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 26, 2012 Apr 26, 2012

I have tried with your expression:--

But some time it shows invalid msg for special characters .

Not all time you give .

You can try with following code:--

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

xmlns:s="library://ns.adobe.com/flex/spark"

xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"

xmlns:local="*">

<fx:Script>

<![CDATA[

private var exp106:String = "^[a-zA-Z0-9 \-_]*$"

private function button1_clickHandler(event:MouseEvent):void{

regValidator.validate();

}

]]>

</fx:Script>

<fx:Declarations>

<!-- Place non-visual elements (e.g., services, value objects) here -->

<mx:RegExpValidator id="regValidator" expression=""

noMatchError="Not valid" source=""

flags="g" property="text" required="true" requiredFieldError="Please enter

string" />

</fx:Declarations>

<mx:VBox>

<s:TextInput id="textBox" />

<s:Button label="validate" click="button1_clickHandler(event)" />

</mx:VBox>

</s:Application

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 26, 2012 Apr 26, 2012

it would be helpful if you can stop posting Re: RegExp for excluding special characters in a string. in the middle of all you posts

and tell me what test strings are failing when they should pass or pass when they should fail

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 26, 2012 Apr 26, 2012

once I had worked out how to make your code compile

I had to make some small change for some reason I can't get the expression to bind to exp106 but if I hard code the regex into your validator I cna't find any cases that doesn't wrk as intended.

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

   xmlns:s="library://ns.adobe.com/flex/spark"

   xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" xmlns:local="*">

          <fx:Script>

          <![CDATA[

                    [Bindable]

                    private var exp106:String = "^[a-zA-Z0-9 \-_]*$";

 

                    private function button1_clickHandler(event:MouseEvent):void{

                              regValidator.validate();

                    }

          ]]>

          </fx:Script>

          <fx:Declarations>

          <!-- Place non-visual elements (e.g., services, value objects) here -->

                    <mx:RegExpValidator id="regValidator"

                              expression="^[a-zA-Z0-9 \-_]*$" source="{textBox}"

                              flags="g" property="text" required="true" requiredFieldError="Please enter string"/>

          </fx:Declarations>

 

          <mx:VBox>

                    <s:TextInput id="textBox" />

                    <s:Button label="validate" click="button1_clickHandler(event)" />

          </mx:VBox>

</s:Application>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 26, 2012 Apr 26, 2012

Hi

Thanks for the help.

I have tried the expression ^[a-zA-Z0-9 \-_]*$

And this is working as per requirements but when it is declared within mxml

tag of RegExpValidator.

Actually I have to use this condition at many places and that's why I have

marked it as "Static Const".

So please tell me a way by which I can avoid the problem of "not properly

working of this expression when taken as variable"..

It is working when :--

<mx:RegExpValidator id="regExpValidator"

source="" property="text" expression="^[a-zA-Z0-9 \-_]*$" noMatchError="" enabled="false"/> But it is not working properly when the condition:-- <mx:RegExpValidator id="regExpValidator" source="" property="text"

expression="{TCConstants.TEMPLATE_NAME_REGULAR_EXPRESSION}"

noMatchError=""

enabled="false"/>

Thank you once again...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 27, 2012 Apr 27, 2012

I found a way to move the regex into a variable

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

   xmlns:s="library://ns.adobe.com/flex/spark"

   xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" xmlns:local="*">

          <fx:Script>

          <![CDATA[

                    private static const exp106:RegExp = /^[a-zA-Z0-9 \-_]*$/;

 

                    private function button1_clickHandler(event:MouseEvent):void{

                              regValidator.validate();

                    }

          ]]>

          </fx:Script>

          <fx:Declarations>

          <!-- Place non-visual elements (e.g., services, value objects) here -->

                    <mx:RegExpValidator id="regValidator"

                              expression="{exp106.source}" source="{textBox}"

                              flags="g" property="text" required="true" requiredFieldError="Please enter string"/>

          </fx:Declarations>

 

          <mx:VBox>

                    <s:TextInput id="textBox" />

                    <s:Button label="validate" click="button1_clickHandler(event)" />

          </mx:VBox>

</s:Application>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 27, 2012 Apr 27, 2012
LATEST

Hey many many thanks for solving this issue.

Once again thanks to you a lot

Regards

Munira

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines