Solved

javascript only a-z A-Z and while space

Posted on 2006-10-24
5
320 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

867 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now