JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 4th, 2008, 10:30 AM
acidedge2004 acidedge2004 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Posts: 139 acidedge2004 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 32 m 12 sec
Reputation Power: 6
Validation of string difficulties

Hi, I was given some brilliant help on this forum with my last post so thought I would try again.

I have this code:
Code:
<html>
<head>

<script type="text/javascript">
function Prompt_hello()
{
var name=prompt("Please enter your name","");
try
{
if name (!isNaN)
	throw=err1;
else if (name=null)
	throw err2;
else if (name="")
	throw err3;
}
catch (er)
{
if (er=="err1")
	alert("Please only enter valid characters");
if (er=="err2" || er=="err3")
	alert("Please enter a name");
}


//if (name!=null && name!="")
{
alert("Hi there, " + name);
}
}
</script>

</head>
<body>

<input type="button" onclick="Prompt_hello()" value="Hello! Please Enter your name" />

</body>
</html>


As you can see, I am basically wanting a prompt to appear so the user can enter a name and have an alert box appear greeting them with their name. This worked with no problem when just using the simple validation below:
Code:
if (name!=null && name!="")


However, I thought I would make it a bit more complex, just as a learning matter than any real need, and decided to use, Catch...try and throw validation. I now cannot get it to work.

I figure I am either using the try, catch functions wrongly, or, and I guess this is most likely, the character validation function is incorrect.

I saw the !isNaN on another script when looking online and thought I would give it a go, as the other validation scripts are far to advanced for me at the moment, so I wanted something simple for what should be a simple script. The purpose was to heck that only characters, a-z, have been used. (any other ways to validate this would also be great, as I wanna learn all the alternatives).

If anyone can point me in the right direction, or let me know where I have gone wrong, I would be very grateful.

Cheers, Mike

Reply With Quote
  #2  
Old January 4th, 2008, 11:01 AM
RyanT RyanT is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2006
Posts: 71 RyanT User rank is Sergeant (500 - 2000 Reputation Level)RyanT User rank is Sergeant (500 - 2000 Reputation Level)RyanT User rank is Sergeant (500 - 2000 Reputation Level)RyanT User rank is Sergeant (500 - 2000 Reputation Level)RyanT User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 2 h 3 m 32 sec
Reputation Power: 13
isNaN is a function so you have to give it something to check

Code:
(isNaN(name)==1) //Name is blank
Comments on this post
vbrtrmn agrees!

Reply With Quote
  #3  
Old January 5th, 2008, 03:22 AM
vbrtrmn's Avatar
vbrtrmn vbrtrmn is offline
4:04 Time Not Found
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2004
Location: Northern Virginia
Posts: 2,273 vbrtrmn User rank is Major (30000 - 40000 Reputation Level)vbrtrmn User rank is Major (30000 - 40000 Reputation Level)vbrtrmn User rank is Major (30000 - 40000 Reputation Level)vbrtrmn User rank is Major (30000 - 40000 Reputation Level)vbrtrmn User rank is Major (30000 - 40000 Reputation Level)vbrtrmn User rank is Major (30000 - 40000 Reputation Level)vbrtrmn User rank is Major (30000 - 40000 Reputation Level)vbrtrmn User rank is Major (30000 - 40000 Reputation Level)vbrtrmn User rank is Major (30000 - 40000 Reputation Level)vbrtrmn User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 22 h 10 m
Reputation Power: 337
Send a message via ICQ to vbrtrmn Send a message via AIM to vbrtrmn Send a message via MSN to vbrtrmn Send a message via Yahoo to vbrtrmn
I RyanT's solution is good .. though how about revamping your code a bit..
Code:
function Prompt_hello() {
  var name=prompt("Please enter your name","");
  var err;
  if (name.match(/^\d+$/) || name.match(/^[\W\D]+$/)) {
    // Name only contains numbers.
    // OR
    // Name only contains non-word and non-digit characters
    err = 'Please only enter valid characters.';
  } else if (name=='') {
    // name is empty
    err = 'Please enter a name.';
  }
  
  if (err.length>0) {
    alert(err);
    // Re-run the function, if you want.
    // Prompt_hello();
  } else {
    alert("Hi there, " + name);
  }
}
__________________
I am so smart, I am so smart, S.M.R.T ... I mean S.M.A.R.T.

Stop Using Pop-Ups

Reply With Quote
  #4  
Old January 7th, 2008, 03:40 AM
acidedge2004 acidedge2004 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Posts: 139 acidedge2004 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 32 m 12 sec
Reputation Power: 6
Tried your suggstions

hi vbrtrmn, Thanks for the code, but it doesn't fully work. Even when I enter a name it still throws me an error. "please enter only valid characters." But thanks for the validation part, I have seen it coded that way in scripts online.

1. Do you always have to use the name.match(/^[\W\D]+$/) twice, once for numbers and characters, or will once do?

2. Are those characters in the parenthesis fixed, or can you add/subtract them depending on what you will allow?

Here is my code as it looks now:
Code:
<html>
<head>

<script type="text/javascript">
function Prompt_hello()
{
var name=prompt("Please enter your name:","");
try
{
if (!isNaN(name)==1) //Name is blank
	throw "err1";
else if (name=null)
	throw "err2";
else if (name="")
	throw "err3";
else 
	alert("Hi there, " + name);
}
catch (er)
{
if (er=="err1")
	alert("Please only enter valid characters err1");
if (er=="err2")
	alert("Please enter a name err2");
if (er=="err3")
	alert("Please enter a name err3");



//if (name!=null && name!="")
}

}
</script>

</head>
<body>

<input type="button" onclick="Prompt_hello()" value="Hello! Please Enter your name" />

</body>
</html>


I have added RyanT's line of code.

I brought the "alert("hi there..." part in to the if...else section as it was being called regardless of being thrown an error. I also made the err2 and err 3 separate lines of code.

Probs:
1: entering invalid characters such as @ stills gives the "Hi there..." alert
2. leaving the name blank or entering numbers throws err1 but no others, no biggie, I can just have the first err as a general one
3. name variable is not passing to the alert box. It comes up "hi there, "

Thanks for all the help I have received so far,
Mike

Reply With Quote
  #5  
Old January 7th, 2008, 12:28 PM
vbrtrmn's Avatar
vbrtrmn vbrtrmn is offline
4:04 Time Not Found
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2004
Location: Northern Virginia
Posts: 2,273 vbrtrmn User rank is Major (30000 - 40000 Reputation Level)vbrtrmn User rank is Major (30000 - 40000 Reputation Level)vbrtrmn User rank is Major (30000 - 40000 Reputation Level)vbrtrmn User rank is Major (30000 - 40000 Reputation Level)vbrtrmn User rank is Major (30000 - 40000 Reputation Level)vbrtrmn User rank is Major (30000 - 40000 Reputation Level)vbrtrmn User rank is Major (30000 - 40000 Reputation Level)vbrtrmn User rank is Major (30000 - 40000 Reputation Level)vbrtrmn User rank is Major (30000 - 40000 Reputation Level)vbrtrmn User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 22 h 10 m
Reputation Power: 337
Send a message via ICQ to vbrtrmn Send a message via AIM to vbrtrmn Send a message via MSN to vbrtrmn Send a message via Yahoo to vbrtrmn
I didn't test my code very well... per your problems, can you describe the "rules" you want to meet for a name? For instance which characters are valid?
A-Z,a-z,0-9,@,???

What else describes a name:
-Cannot contain just numbers
-Cannot be empty or just contain spaces
-???

Reply With Quote
  #6  
Old January 8th, 2008, 05:55 AM
acidedge2004 acidedge2004 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Posts: 139 acidedge2004 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 32 m 12 sec
Reputation Power: 6
Well, it is just basic validation. The name should basically only be A-z, not be blank or null. Just I can add @ at the mo and the alert still shows, so it is accepted using the validation rules you gave me.

The name variable is also not passing to the alert, so have to sort that out to. what does err.length>0 mean? does the length object not measure a strings length? I am a complete JS noob, so excuse all the questions.

Thanks, Mike

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Validation of string difficulties

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap