|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Validate numbers
Hi all. I would like to validate a number a user enters into my form. This number can be 5 or 6 numerical numbers or have 6 numeric numbers seperated by a dash with 6 more numeric numbers like so 123456-123456. How can I do this. I'm stumped on this one. Please help me.
Tracy |
|
#2
|
|||
|
|||
|
Are the numbers randomly generated?
|
|
#3
|
||||
|
||||
|
Quote:
Regular expressions. /^(?:\d{5,6}|\d{6}-\d{6})$/ will match the pattern you described. Refer to Mozilla's Developer Section to see how to match a regular expression against a string. |
|
#4
|
|||
|
|||
|
Here is what I have so far but I can't get it to work. Please help.
Tracy Code:
<script>
function validateCharge(theForm){
//VALIDATE CHARGE
if(theForm.charge){
if(!theForm.charge.value.match(/^\d{5}$/) || !theForm.charge.value.match(/^\d{6}$/)){
alert("Please input a valid Charge Account!")
theForm.charge.style.backgroundColor='pink';
theForm.charge.focus();
return false
}}
return (true);
}
</script>
<form method="POST" name="theForm" onsubmit="return validateCharge(theForm)" action="--WEBBOT-SELF--">
<input type="text" name="charge" size="20"></p>
<p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>
|
|
#5
|
||||
|
||||
|
The biggest problem with your code is here:
Code:
<form method="POST" name="theForm" onsubmit="return validateCharge(theForm)" action="--WEBBOT-SELF--"> Change onsubmit="return validateCharge(theForm)" to onsubmit="return validateCharge(this)" and it might start rejecting input of anything other than five or six consecutive digits. |
|
#6
|
||||
|
||||
|
Code:
return (true); is useless and redundant. If no certain value returned, the function returns undefined, which will allow the submit same as a true returned, because the only value which prevents the submit action is false ex: onsubmit="return false"
__________________
HELP SAVE ANA |
![]() |
| Viewing: Dev Shed Forums > Web Design > JavaScript Development > Validate numbers |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|