form validation no characters allowed

This is a discussion on "form validation no characters allowed" within the JavaScript Forum section. This forum, and the thread "form validation no characters allowed are both part of the Program Your Website category.



 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1  
Old Feb 29th, 2008, 04:02
New Member
Join Date: Feb 2008
Location: Phoenix,AZ
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
form validation no characters allowed

I would like to add code so that FirstName and LastName fields will only allow letters, numbers and underscores.

I found this code, but don't know how to edit the javascript below that works well with my existing form.

Any help would be greatly appreciated.

Thanks

-------------------------------------
var illegalChars = /\W/;
// allow only letters, numbers, and underscores
if (illegalChars.test(strng)) {
error = "The username contains illegal characters.\n";
}
------------------------------------



Don't know how to add the above to work with this...

<script LANGUAGE="javascript">
<!--
function validation(){
var path = document.mainform
if (path.password.value != path.password2.value){
alert('Your passwords didn`t match. Try again');
path.password.value = '';
path.password2.value = '';
return false
}
else if (path.password.value == '' || path.City.value == '' || path.Zip.value == '' || path.State.value == '' || path.Address.value == ''|| path.Telephone.value == ''|| path.FirstName.value == '' || path.LastName.value == '' || path.Email.value == ''){
alert('You must fill out the required fields.')
return false
}
else if (path.question.options[path.question.selectedIndex].value == '' || path.answer.value == ''){
alert('You must pick a question and fill out an answer for lost password.')
return false
}
else{
return true
}

}
//-->
</script>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Mar 5th, 2008, 21:35
Junior Member
Join Date: Mar 2008
Location: Torquay, Devon, UK
Age: 24
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Re: form validation no characters allowed

Give this a try. You can't go wrong with good old RegEx!
If any names are wrong, change as needed.

Code: Select all
else if (!(path.FirstName.value.match(/[A-Za-z0-9_]/)) || !(path.LastName.value.match(/[A-Za-z0-9_]/))){
alert('You can't enter any special characters in the first name or last name fields!')
return false
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Mar 5th, 2008, 23:38
New Member
Join Date: Feb 2008
Location: Phoenix,AZ
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Re: form validation no characters allowed

I added that, with the complete version below. Doesn't seem to work because the form submits and ignores the added code.
I appreciate your help.


<script LANGUAGE="javascript">
<!--
function validation(){
var path = document.mainform
if (path.password.value != path.password2.value){
alert('Your passwords didn`t match. Try again');
path.password.value = '';
path.password2.value = '';
return false
}
else if (path.password.value == '' || path.City.value == '' || path.Zip.value == '' || path.State.value == '' || path.Address.value == ''|| path.Telephone.value == ''|| path.FirstName.value == '' || path.LastName.value == '' || path.Email.value == ''){
alert('You must fill out the required fields.')
return false
}
else if (path.question.options[path.question.selectedIndex].value == '' || path.answer.value == ''){
alert('You must pick a question and fill out an answer for lost password.')
return false
}
else if (!(path.FirstName.value.match(/[A-Za-z0-9_]/)) || !(path.LastName.value.match(/[A-Za-z0-9_]/))){
alert('You can`t enter any special characters in the first name or last name fields!')
return false
}
else{
return true
}
}
//-->
</script>

Last edited by azphoenix; Mar 6th, 2008 at 06:55.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Mar 6th, 2008, 08:25
Junior Member
Join Date: Mar 2008
Location: Torquay, Devon, UK
Age: 24
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Re: form validation no characters allowed

Sorry, poor logic on that! (unencapsulated OR)
This one looks like a mess of brackets, but its logic is good.


else if(!((document.getElementById("txtFirstName").value.match(/^(\w){1,}$/)) && (document.getElementById("txtLastName").value.match(/^(\w){1,}$/)))){alert("You can't use special characters in the First or Last name fields!"); return false;}


Replace "txtFirstName" and "txtLastName" with the ID attributes of the relevant text boxes.

-crosses fingers-

Last edited by RSoftware; Mar 6th, 2008 at 08:31. Reason: The code box wasn't showing the code.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Mar 6th, 2008, 18:45
New Member
Join Date: Feb 2008
Location: Phoenix,AZ
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Re: form validation no characters allowed

Thanks for the code; it works great!

Forever, in trying the code I discovered 2 scenarios that will cause me trouble that I forgot about.

(1) I need to allow for spaces in those 2 fields. For example, if the first name is "Mary Jo", it won't be allowed because of the space. Could potentially have that same issue with the last name too.

(2) I also need to allow for " ' " just incase someone's last name is "O'Neal" etc. I can do an asp replace function to " ` " on the post page before it goes to the database. I don't know if its possible, but it would be good if the java script code would only allow one " ' ". That way it won't submit if the user tries to add a nickname like John 'Jack'.

Again, thanks for the code and if there is a fix for the above 2 scenarios, I appreciate your help.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Mar 6th, 2008, 20:33
Junior Member
Join Date: Mar 2008
Location: Torquay, Devon, UK
Age: 24
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Re: form validation no characters allowed

easy enough, replace the ^(\w){1,}$ regex with ^([a-zA-Z0-9'_ -]){1,}$ in both halves of the if.
I recommend reading up on RegEx syntax and the Javascript string::match function. They can prove invaluable in quickly validating user input.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Mar 6th, 2008, 20:56
New Member
Join Date: Feb 2008
Location: Phoenix,AZ
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Re: form validation no characters allowed

Thank you very much. That's works great! I will do some reading up on that; thanks for the suggestion.

Is there a way to only allow the use of one " ' " or is that beyond the scope of what javascript can do?

Thanks again.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Mar 6th, 2008, 22:47
Junior Member
Join Date: Mar 2008
Location: Torquay, Devon, UK
Age: 24
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Re: form validation no characters allowed

There seems to be no native way to do it, so below i've written a simple function to return the number of occurances of a string within another string. Enjoy ^^.
Code: Select all

function countStr(strSource, strTest){
return (strSource.split(strTest).length -1);
}
Then add the following elseif clause:

Code: Select all
else if(!((countStr(document.getElementById('txtFirstName').value, "'")<=1) && (countStr(document.getElementById('txtLastName').value, "'")<=1))){
alert("You can only use one apostrophe (') in the First or Last name fields!"); return false;
}
Hope that works!

Last edited by RSoftware; Mar 6th, 2008 at 22:51. Reason: typo
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9  
Old Mar 6th, 2008, 23:06
New Member
Join Date: Feb 2008
Location: Phoenix,AZ
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Re: form validation no characters allowed

Not sure if I added it correctly to the javascript. Below in red is what I added. If I had to guess, I didn't add the function part correctly.

<script LANGUAGE="javascript">
<!--
function validation(){
var path = document.mainform
if (path.password.value != path.password2.value){
alert('Your passwords didn`t match. Try again');
path.password.value = '';
path.password2.value = '';
return false

}
function countStr(strSource, strTest){
return (strSource.split(strTest).length -1);
}
else if (path.password.value == '' || path.City.value == '' || path.Zip.value == '' || path.State.value == '' || path.Address.value == ''|| path.Telephone.value == ''|| path.FirstName.value == '' || path.LastName.value == '' || path.Email.value == ''){
alert('You must fill out the required fields.')
return false
}
else if (path.question.options[path.question.selectedIndex].value == '' || path.answer.value == ''){
alert('You must pick a question and fill out an answer for lost password.')
return false
}

else if(!((document.getElementById("FirstName").value.m atch(/^([a-zA-Z0-9'_ -]){1,}$/)) && (document.getElementById("LastName").value.match(/^([a-zA-Z0-9'_ -]){1,}$/)))){alert("You cannot use special characters in your name. Please no nicknames.");
return false;
}

else if(!((countStr(document.getElementById('FirstName' ).value, "'")<=1) && (countStr(document.getElementById('LastName').v alue, "'")<=1))){alert("You can only use one apostrophe (') in the First or Last name fields!");
return false;
}
else{
return true
}
}
//-->
</script>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10  
Old Mar 6th, 2008, 23:45
Junior Member
Join Date: Mar 2008
Location: Torquay, Devon, UK
Age: 24
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Re: form validation no characters allowed

You have the function nested in your ifstatment as though it were a clause. Move it up outside the function:

function countStr(strSource, strTest){
return (strSource.split(strTest).length -1);
}
function validation(){
var path = document.mainform
if (path.password.value != path.password2.value){
alert('Your passwords didn`t match. Try again');
path.password.value = '';
path.password2.value = '';
return false
}

Nesting a function within another function is fine, it makes that function available only to the function within which it is declared.

Nesting a function within an if will just kill your if.
if syntax must follow: if - [else if] - [else] - end.

Keep going, you'll be a master in no time
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #11  
Old Mar 6th, 2008, 23:47
Junior Member
Join Date: Mar 2008
Location: Torquay, Devon, UK
Age: 24
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Re: form validation no characters allowed

also there appears to be a space after the ID of your marked elseif:
else if(!((countStr(document.getElementById('FirstName' ).value, "'")<=1)

This may or may not throw an error, but i recommend fixing.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #12  
Old Mar 7th, 2008, 00:53
New Member
Join Date: Feb 2008
Location: Phoenix,AZ
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Re: form validation no characters allowed

You are brilliant! It works great.

I know enough about ASP in that I should of caught the nested if statement.

I wish I was the master in javascript like you. You have been a big help and I truly appreciate your assistance in this matter.

Take care!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #13  
Old Mar 7th, 2008, 03:34
New Member
Join Date: Feb 2008
Location: Phoenix,AZ
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Re: form validation no characters allowed

One more question....Would you know why this javascript doesn't work in Firefox. I do have javascript enabled.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #14  
Old Mar 7th, 2008, 11:19
Junior Member
Join Date: Mar 2008
Location: Torquay, Devon, UK
Age: 24
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Re: form validation no characters allowed

Is it just the most recently added function / if clause that is causing it not to work?

Does firefox raise an error? (Check the 'Tools' > 'Error Console' box.)

If there's an error there, post it and i'll see if i can work out the solution on that, otherwise, you can attach the page if you like and i'll see what i can do.

Cross-browser compatability is the bane of the internet coder!

For now, to work! ttyl =]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Read First 3 characters and last 4 characters of string JustinStudios PHP Forum 2 Apr 4th, 2008 00:01
AS Form / Validation Help papalazarou78 Flash & Multimedia Forum 0 Jul 31st, 2007 19:43
Form Validation cjrollo Flash & Multimedia Forum 0 Feb 22nd, 2007 17:33
PHP Form Validation. kaz PHP Forum 2 Jul 22nd, 2006 20:47
Form validation brittny JavaScript Forum 2 Jul 8th, 2005 19:46


All times are GMT. The time now is 05:56.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42