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 12:22 AM .
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>
All code given is free and non-refundable.
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>
All code given is free and non-refundable.
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 ( i = 0 ; i < kw . 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 ; }
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;
}
All code given is free and non-refundable.
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
Forum Rules
Bookmarks