SunQuest
           HTML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsWeb DesignHTML Programming

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 July 12th, 2000, 03:21 PM
simpson simpson is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Posts: 32 simpson User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Hi, I'm trying to check if the user entered a number that is Positive.....
now they are allowed to enter 10,000
or $10,000 .....is there an easy way
to check for valid input?

Reply With Quote
  #2  
Old July 13th, 2000, 10:32 PM
billyo billyo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Posts: 114 billyo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
I think you should read an article on this site: http://www.devshed.com/Server_Side/...tration/RegExp/ but in my infinite boredom I created a regular expression that will only accept input of the form (dollar sign or not) number (comma or not) number. The format for this code is an adaptation of that given in the above article:
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
<script language=JavaScript><!--
function Validate(obj)
{
// obtain form value into variable
var amount = obj.yourinput.value;

// define regex
var pattern = /(^[$d])+([,d])+([d])$/;
/*I'm still very new to regex, but the above
pattern says: it can only start with a
number or a dollar sign. Then it says: the
next character could be a comma or it could
be a number. (note that it can only be a
comma if it isn't the first character. This
prevents the user from entering ,000) The
last part just says it has to end with a number.*/

// test for pattern
flag = pattern.test(amount);

// pattern.test returns a boolean
if(flag)
{
alert("Pattern Matches");
return true;
}
else
{
alert("Pattern Does Not Match");
return false;
}
}
--></script>
</head>
<body>
<!--some where in the HTML you'll have-->

<form onSubmit="return Validate(this);">

<input name="yourinput" type="text">

<input type="submit">

</form>
</body>
</html>[/code]
RegEx is pretty cool, but it can easily lead to infuriating frustration... Note too that the above code doesn't actually check to see if a number is positive, it just makes sure that it doesn't start with a "-" character.

[This message has been edited by billyo (edited July 13, 2000).]

[This message has been edited by billyo (edited July 13, 2000).]

Reply With Quote
  #3  
Old July 14th, 2000, 01:08 AM
billyo billyo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Posts: 114 billyo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Actually, I just figured out how to create regular expressions "on the fly" try this page out.
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre><html>
<head>
<script language=JavaScript><!--

function Validate()
{
// this'll construct a regex from a variable
var reg=new RegExp(document.all.test_reg.value)

// obtain form value into variable
var thing= document.all.thing_to_test.value;

// define regex
//for currency var pattern = /(^[$d])+([,d])+([d]$)/;
//for acceptable extensions var pattern = /.jpg$|.gif$|.png$|.tif$/

// test for pattern
flag = reg.test(thing);

// pattern.test returns a boolean
if(flag)
{
document.all.results.value="pattern matches"
return true;
}
else
{
document.all.results.value="pattern doesn't match"
return false;
}
}
--></script>
<style><!--
td, button
{
font-size:10px;
font-family:Arial;
}
--></style>
</head>
<body bgcolor=0099ff>
<table bgcolor=000000><tr><td>
<table bgcolor=ffff00>
<tr><td colspan=2>
Regular Expression Tester
</td></tr>
<tr><td>
regex to test
</td><td>
<input name="test_reg" type="text">
</td></tr>
<tr><td>
string to test
</td><td>
<input name="thing_to_test" type="text">
</td></tr>
<tr><td>
result
</td><td>
<input name="results" type=text>
</td></tr>
<tr><td colspan=2>
<button style="background-color:0099ff;font-weight:bold;width:220;" onclick="Validate();">Test The RegEx Pattern</button>
</td></tr>
</table>
</td></tr></table>
</body>
</html>[/code]

Reply With Quote
  #4  
Old July 14th, 2000, 01:47 PM
CPT COCKSUCKER CPT COCKSUCKER is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2000
Posts: 4 CPT COCKSUCKER User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to CPT COCKSUCKER
if(document.formName.elementName.value.charAt(0) == '-')
alert('you are a pickle smoocher');

[This message has been edited by CPT COCKSUCKER (edited July 14, 2000).]

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignHTML Programming > form validation


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 | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway