Avatar of BigSkyCountry
BigSkyCountryFlag for United States of America asked on

Need help with field validation in a html page using Javascript

I have a webform in html that has 1 field - a number that on submitting the form is passed on to a script to generate a page.  Is there a simple (or not) javascript that can ensure the value entered is a numeric so that a sql error doesn't occur?
html on the page is like this - passes a hidden form value and an entered form value to a script.

<FORM ACTION = ""www.mywebsite.com/test/scriptname" Method="Post">
<input type="hidden" name="type" value="V">
<input type="text" name="productid" maxlength="6">
<input type="submit" value="get product"  name="get product">

If a user enters an alpha character = we get ugly sql errors.
JavaScriptHTML

Avatar of undefined
Last Comment
BigSkyCountry

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Justin Mathews

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
crisco96

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
BigSkyCountry

Hello and thank you for both of your responses- I have a meeting I have to go to and then I will try both-  
ASKER
BigSkyCountry

I created 2 forms with both sets of code.  Doesn't appear as if the function fires on submit - a numeric value works OK but an alpha still splatters the sql errors.  I first had the pages as .htm in the page directory (read only) - then thought maybe they needed to be executed so I moved to a script directly as .aspx - but same result.    (entering an 'a' gets sql error 'invalid column name 'a')  Any suggestions would be appreciated.  (really need to rewrite this whole website :(
Justin Mathews

Can you post your HTML source?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER
BigSkyCountry

Sure - you can access the test page at"www.mywebsite.com/test/scriptname.   A valid product id number would be 546320 - any alpha cause the error - much thanks
Justin Mathews

The form says onsubmit function is validate_form:

<FORM ACTION=""www.mywebsite.com/test/scriptname"  METHOD="POST" onsubmit="return validate_form(this)" >

But the JS function you have defined is named validate:

function validate(frm)
{
 if(!frm.productid.value.match(/^[\d\.]+$/))
{
alert("invalid productid")
return false;
}
return true;
}

The names should match. Make both of them either validate_form or validate.
ASKER
BigSkyCountry

Sorry - I should have caught that - I 'll take a fresh look tomorrow and see if there are more typos that I might have missed.

Thank you again for your help.  
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.