www.webdeveloper.com
Results 1 to 7 of 7

Thread: regular expression on javascript

  1. #1
    Join Date
    Aug 2011
    Location
    Hong Kong
    Posts
    49

    regular expression on javascript

    i want to validate an input value using the following function which included a regular expression. And my criteria of the value is:

    1) any keyword with characters 'a-z', 'A-Z', '0-9', '-', '_', but the first character of the keyword must be without hyphen and underscore;

    2) at most 3 keywords in the html input, with one comma separating each other


    PHP Code:
    function checkKeywords(str){
        var 
    re=/^([a-zA-Z0-9]{1})([a-zA-Z0-9-_]+)(([\,]{1})([a-zA-Z0-9]{1})([a-zA-Z0-9-_]+))?(([\,]{1})([a-zA-Z0-9]{1})([a-zA-Z0-9-_]+))?$/;
        return 
    re.test(str);
    }

    if(
    doucment.form.keywords.value !="" && !checkKeywords(doucment.form.keywords.value)){
        return 
    false;

    PHP Code:
    html code:
    <form onSubmit="checkKeywords();">
    <input type="text" name="keywords" id="keywords" value="<?php echo stripslashes(htmlentities($_POST['keywords'])); ?>" />
    </form>
    the problem should come from the wrong regular expression. I am sorry for the long clumsy code because i wrote it all by myself

    Your help is very much appreciated
    Last edited by jeffap; 08-06-2012 at 01:22 AM.

  2. #2
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    It's a little longer, but this is what I would do:

    Code:
    function checkKeywords(str)
    {
    	var kw = String(str).split(','), i;
    	if (kw.length > 3) {
    		alert('Too many keywords!');
    		return false;
    	} else {
    		for (i = 0; i < kw.length; ++i) {
    			if (!(/^[a-z0-9]{1}[\w\-]*$/i).test(kw[i])) {
    				alert('Invalid keyword: ' + kw[i]);
    				return false;
    			}
    		}
    	}
    	return true;
    }
    Don't forget to also return the result of the check to your form:

    Code:
    <form onSubmit="return checkKeywords(this.keywords.value);">
    <input type="text" name="keywords" id="keywords" value="<?php echo stripslashes(htmlentities($_POST['keywords'])); ?>" />
    </form>

  3. #3
    Join Date
    Aug 2011
    Location
    Hong Kong
    Posts
    49
    hi bionoid, your code is neat, clean and easy to understand, much better than mine

    i have a question here, when you define var kw

    var kw = String(str).split(','), i;

    can javascript recognize "String"?

    i tried put it in my .js but it does not work

    i actually put it in a "check" function for all input, like:

    PHP Code:
    function check(form){
        if(
    bla bla bla){
            
    bla bla bla;
            return 
    false;
        }
        if(
    form.keywords.value !=""){
            ***
    your function???***
        }
        return 
    true;
    }

    html:

    <
    form onSubmit="return check(this);">
    .
    .
    .
        <
    input name="keywords">
    .
    .
    .
    </
    form


    ps: i want to check out some fundamental javascript rules because i think i always make mistakes and waste lots of time to fix them. any simple (for dummy) website for me

  4. #4
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    If you can post all the code/html you are working on, or if you have it online, I could see where the problem is.

    This is the code I used to experiment with your question:

    Code:
    <!DOCTYPE html>
    <html lang="en">
    	<head>
    		<meta charset="utf-8" />
    		<title>Check Input</title>
    
    		<script type="text/javascript">
    
    			function checkKeywords(str)
    			{
    				var kw = String(str).split(','), i;
    				if (kw.length > 3) {
    					alert('Too many keywords!');
    					return false;
    				} else {
    					for (i = 0; i < kw.length; ++i) {
    						if (!(/^[a-z0-9]{1}[\w\-]*$/i).test(kw[i])) {
    							alert('Invalid keyword: ' + kw[i]);
    							return false;
    						}
    					}
    				}
    				return true;
    			}
    
    		</script>
    
    	</head>
    	<body>
    
    		<form onSubmit="return checkKeywords(this.keywords.value);">
    			<input type="text" name="keywords" id="keywords" value="123,-456,789" />
    			<input type="submit" />
    		</form>
    
    	</body>
    </html>

  5. #5
    Join Date
    Aug 2011
    Location
    Hong Kong
    Posts
    49
    the whole php and js code are pretty long so i will put what it is relevant

    PHP Code:
    <html>
    <head>
            <script type="text/javascript" src="checksell.js"></script>        
    </head>
    <body>
        <form action="verify.php" method="post" target="_self" onSubmit="return checkForm(this);">
            <input type="text" name="keywords" value="<?php echo stripslashes(htmlentities($_POST['keywords'])); ?>" />
            <input name="submit" type="submit" value="submit" />
    </form>
    </body>
    </html>
    PHP Code:
    //checksell.js

    function checkForm(form){
        if(
    form.keywords.value !=""){
            function 
    checkKeywords(str){
                var 
    kw String(str).split(','), i;
                if (
    kw.length 3){
                    
    alert('too many keywords');
                    
    form.keywords.focus();            
                    return 
    false;
                }else{
                    for (
    0kw.length; ++i) {
                        if (!(/^[
    a-z0-9\u4e00-\u9fa5]{1}[\w\-\u4e00-\u9fa5]*$/i).test(kw[i])){
                            
    alert('invalid keywords');
                            
    form.keywords.focus();                        
                            return 
    false;
                        }
                    }
                }
            }
        }
        return 
    true;


  6. #6
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Try this:

    checksell.js
    Code:
    function checkForm(form)
    {
    	var e = form.keywords, kw, i;
    	if (e.value !== '') {
    		kw = String(e.value).split(',');
    		if (kw.length > 3) {
    			alert('too many keywords'); e.focus(); return false;
    		} else {
    			for (i = 0; i < kw.length; ++i) {
    				if (!(/^[a-z0-9\u4e00-\u9fa5]{1}[\w\-\u4e00-\u9fa5]*$/i).test(kw[i])) {
    					alert('invalid keywords'); e.focus(); return false;
    				}
    			}
    		}
    	} else {
    		alert('please specify a keyword'); e.focus(); return false;
    	}
    	return true;
    }

  7. #7
    Join Date
    Aug 2011
    Location
    Hong Kong
    Posts
    49
    awesome it works
    thank you very much for your help!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles