Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Not allow entry of commas in input field 1

Status
Not open for further replies.

candidar

Programmer
Dec 5, 2003
4
US
I have a form based on a strresult that does a calculation. When a user enters a comma in the coverage_amount field, I get a NaN! calculation. I want to be able to block the comma from being entered, period. Here is a sample if the section I am working on, there is a lot of code, so hopefully this will be enough. Any suggestions would be great!

Cheers!

<!--BEGINNING OF HOME/MOBILE HOME OWNER-->
function ActivateDwelling(territory)
{
divErrorMessages.Print('');
divErrorMessages.Hide();

divEntry.Print('');
divEntry.Hide();

divQuote.Print('');
divQuote.Hide();

divQuoteCondo.Print('');
divQuoteCondo.Hide();

var strResults;

DB1.Coverages.MoveFirst();
DB1.deductable.MoveLast();

strResults = '<table width="600">';

strResults += '<tr>';
strResults += '<td colspan="3">You have select a <font color="#EC1C2D"><b>Home/Mobile Home Owner </font></b> policy for the property located in the <font color="#EC1C2D"><b>' + document.form.zipcode.value + ' </font></b> zip code. <br><br></td>';
strResults += '</tr>';

strResults += '<tr>';
strResults += '<td width="50"><img src="images/step_2.gif" width="50" height="27" alt="Step 2" align="left" valign="middle">&nbsp;</td>';
strResults += '<td width="200"><input name="coverage_amount" size="7" onKeyUp="FinishDwelling(' + territory + ')"></td>';
strResults += '<td width="350">Enter the <a href="_self" onMouseOver="popLayer(5)" onMouseOut="hideLayer()"><b>Insured Value of your home.</b></a> (Do Not Use Commas)</td>';
strResults += '</tr>';

strResults += '<tr>';
strResults += '<td width="50"><img src="images/step_3.gif" width="50" height="27" alt="Step 3" align="left" valign="middle">&nbsp;</td>';
strResults += '<td width="200"><select name="dwellingtype" onChange="FinishDwelling(' + territory + ')">';
strResults += '<option value="91_or_later">Framed construction - 1991 or later</option>';
strResults += '<option value="79_thru_90">Framed construction - 1979 - 1990</option>';
strResults += '<option value="78_or_earlier">Framed construction - 1978 or earlier</option>';
strResults += '<option value="all_other">All other construction</option>';
strResults += '<option value="mobile_home">Mobile home</option>';
strResults += '</select></td>';
strResults += '<td width="350">Select the <a href="_self" onMouseOver="popLayer(6)" onMouseOut="hideLayer()"><b>dwelling type of your home.</a></b></td>';
strResults += '</tr>';

strResults += '<tr>';
strResults += '<td width="50"><img src="images/step_4.gif" width="50" height="27" alt="Step 4" align="left" valign="middle">&nbsp;</td>';
strResults += '<td width="200"><select name="a_deductible" onChange="FinishDwelling(' + territory + ')">';
for(var i = 1; i <= DB1.deductable.Count(); i++)
{
strResults += '<option value="' + DB1.deductable.a_deductible + '">' + DB1.deductable.a_deductible + '</option>';
DB1.deductable.MoveFirst();
}
DB1.deductable.MoveFirst();
strResults += '</select></td>';
strResults += '<td width="350">Please select <a href="_self" onMouseOver="popLayer(7)" onMouseOut="hideLayer()"><b>Coverage A Deductible.</a></b></td>';
strResults += '</tr>';

strResults += '<tr>';
strResults += '<td width="50"><img src="images/step_5.gif" width="50" height="27" alt="Step 5" align="left" valign="middle">&nbsp;</td>';
strResults += '<td width="200"><select name="coverages" onChange="FinishDwelling(' + territory + ')">';
for(var i = 1; i <= DB1.Coverages.Count(); i++)
{
strResults += '<option value="' + DB1.Coverages.coverage_c + '_' + DB1.Coverages.coverage_d + '">';
strResults += 'C: $' + DB1.Coverages.coverage_c + ' D: $' + DB1.Coverages.coverage_d + '</option>';
DB1.Coverages.MoveNext();
}
DB1.Coverages.MoveFirst();

strResults += '</select>';
strResults += '</td>';
strResults += '<td width="350">Please select Personal Property (C) and Loss of Use (D) <a href="_self" onMouseOver="popLayer(0)" onMouseOut="hideLayer(1000000)"><b>coverages</b></a>.</td>';
strResults += '</tr></table>';

divEntry.Show();
divEntry.Print(strResults);
FinishDwelling(territory);
}


 
candidar,

You know those users can always be counted on to mess
things up.

How about stripping the commas out then using the string?

Code:
<html><head><title>TEST</title>
<script language="JavaScript">
function getFloat(str) {
str = str.replace(/,/g,"");
var myInt = (isNaN(parseFloat(str)))? "isNaN" : parseFloat(str);
alert(myInt);
}
</script></head><body>
<input type="text" size="25" onBlur="getFloat(this.value)">
</body></html>

HTH

Great Javascript Resource:
 
Lrnmore,

Thank you for the sample. That's not quite what I am trying to do. I wanted to remove the commas period, which the alert, I think it may confuse people. No offense to elder people, but alot of the people using the site are 50+, and not real computer savy.
 
Yeah,

That was just part of the model, just strip the commas out
of the value with a line like:

theNumber = str.replace(/,/g,"");

Where "str" is content from your input.

That will remove all of the commas the user might enter.

Then you can use parseInt(whole numbers) or
parseFloat(numbers that might include a decimal)
to convert the "text" input into an integer.
 
Thanks, orry I am so tired I didn't even pick it apart! It works great, thanks again!

Cheers!
 
Hi

I have the following validation in my form.

<SCRIPT LANGUAGE="JavaScript1.2">
<!-- Hide from other browsers

function validate(regForm)
{

if (regForm.Firstname.value =="")
{
window.alert("Please enter your Firstname");
return false;
}

}

and my form starts like this..

<form method=post action="submit.php" onsubmit="return validate(this)">

How would I ensure that the Firstname field in my form was not blank or didn't contain commas?

Any help appreciated
 
You're already 50% of the way there... Now you just need to add some comma detection:

Code:
function validate(regForm) {
    if (regForm.Firstname.value == '') {
        window.alert('Please enter your first name');
        return (false);
    }
    if (regForm.Firstname.value.indexOf(',') != -1) {
        window.alert('You cannot enter commas in your first name');
        return (false);
    }
}

Hope this helps,
Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top