|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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? |
|
#2
|
|||
|
|||
|
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).] |
|
#3
|
|||
|
|||
|
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] |
|
#4
|
|||
|
|||
|
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).] |
![]() |
| Viewing: Dev Shed Forums > Web Design > HTML Programming > form validation |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|