SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript regular expressions problem

    hails...
    i have a problem to validate input with javascript. i want to know the input just included with some characters that i want to match it. see below:
    Code JavaScript:
    function checkString(input) {
    	myPattern = /([\a\b\c ])/g;
    	if (input.match(myPattern)) alert('true');
    	else alert('false');
    }
    checkString('a bbb c aaa'); /// alert:true
    checkString('a bbb c aaa 8'); /// also alert:true that i want to alert:false
    give me the true pattern.
    thank.

  2. #2
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.
    Try this:

    function checkString(str) {
    regEx = /^[a-c\s]+$/;
    if (regEx.test(str)) {
    alert('true');
    }
    else{
    alert('false');
    }
    }
    checkString('a bbbb cccc'); /// alert:true
    checkString('a bbb c aaa 8');/// alert:false
    Bye.

  3. #3
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you only allow abcs and spaces you could test for anything ELSE-
    Code:
    function checkString(input) {   
        alert(!/[^a-c ]/.test(input))
    }

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mrhoo View Post
    if you only allow abcs and spaces you could test for anything ELSE-
    Code:
    function checkString(input) {   
        alert(!/[^a-c ]/.test(input))
    }
    no my problem is i have the list of Unicode chacacter for example \u0620, \u06a2 and \u06f3;
    must use one by one and can't use [a-b] method.

    Code JavaScript:
    /**
    * \u0628 = ب
    * \u067e = پ
    * \u062a = ت
    */
    function checkString(input) {
        myPattern = /([\u0628\u067e\u062a ])/g;
        if (input.match(myPattern)) alert('true');
        else alert('false');
    }
    checkString('ب پپپ تتت'); /// alert:true
    checkString('ب پپپ تتت 8');

  5. #5
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Then test for anything else.

    alert(!/[^\u0628\u067e\u062a ]/.test(input));

  6. #6
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function checkString(input) {
    	myPattern = /[^amz ]/;
    	if (!input.match(myPattern)) alert('true');
    	else alert('false');
    }
    checkString('a mm z');
    checkString('a m z 8');
    i don't want to use ! in if. i want to write the myPattern as my work style, you know ?
    give me the true pattern for use in myPattern. why use this method i have a problem?
    thank.
    Last edited by sweb(ir); Sep 4, 2008 at 01:34.

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by sweb(ir) View Post
    i don't want to use ! in if. i want to write the myPattern as my work style
    With !

    Code javascript:
    if (!input.match(myPattern)) alert('true');
    else alert('false');

    Without !

    Code javascript:
    if (input.match(myPattern)) alert('false');
    else alert('true');
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #8
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    myPattern = /^[\u0628\u067e\u062a ]*$/g;


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •