Solved

javascript only a-z A-Z and while space

Posted on 2006-10-24
5
321 Views
Last Modified: 2008-02-26
Hello,

I'm writing a javascript that will validate a form input. only a-z A-Z and while space are
allowed.

var passwdPat=/[^a-zA-Z]\s/g; seems not working well. is there something missing in this pattern?

Thank you so much for your reply,

lilyyan

0
Comment
Question by:lilyyan
  • 3
  • 2
5 Comments
 
LVL 30

Accepted Solution

by:
callrs earned 500 total points
ID: 17800611
<script type="text/javascript">
// Allow spaces and letters only
function validate(frm){
var v=frm.n1.value;
var rx=/^[a-z\s]*$/i;
if(!rx.test(v)) {alert("Spaces and letters only"); frm.n1.focus(); return false;}
else alert("valid");
return false;
}

</script>
<form onsubmit="return validate(this)">
<input name=n1 value="abcd123">
<input type=submit value="Submit">
</form>
0
 

Author Comment

by:lilyyan
ID: 17800733
hi,

thanks for your reply. it works great!

I'm trying to understand the expression. i'm relative new to exprssion.

/^[a-z\s]*$/i;

i : means ignore the letter case ?

anything between / / is a pattern ?

[a-z\s ] means any input letter could be a-z or space?

* means 0 to any number

$ means ?

Thanks for your reply,

lilyyan
0
 
LVL 30

Expert Comment

by:callrs
ID: 17800766
i  - case insensitive
*  -  0 or more
+  -  1 or more
?   - 0 or 1, or it can mean non-greedy, or...
^  -  starts with, or inside square brackets it means NOT, e.g. [^a-z] means match if it not a-z
$  - ends with

http://www.experts-exchange.com/Q_21995615.html  repalce parameters
Getting started with Regular Expressions: http://www.experts-exchange.com/Q_21980291.html 
0
 

Author Comment

by:lilyyan
ID: 17800790
so /^[a-z\s]*$/i;  means the valid input is any letter (0 or more ) starts with a-z or space
and ends with  a-z or space. is this correct?

thanks for your reply!
0
 
LVL 30

Expert Comment

by:callrs
ID: 17801086
It's an expression that checks that a string starts and ends with ONLY 0 or more bytes of a-z A-Z space. If you had 'a1b', the test is false since the first 3 bytes ('a1b') has a 1, and the last 3 bytes (again 'a1b') has a 1.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question