Form field Validation using java script and Struts Form Bean Validation() method

hi all,

I have a form field ( text box ), i want to limit the input to numbers that too between 1-255 only.
How can i validate the form field to control the user input?.

How can we do in java script?
How can we control the same in Struts - Form Bean i.e Validate() method?

Can anyone provide code snippest?

Thanks,
Ram
LVL 2
ramsin112400Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

intreegCommented:
Here is an article on using REgular Expressions in Javascript:

How to Use The JavaScript RegExp Object
http://www.regular-expressions.info/javascript.html

I believe the pattern would be ^[1-255]{1,3}$




0
jespiresCommented:

Here is a snipplet of code.  Please let me know if this is enough for you

<script language =javascript>
function Validate_Form()
      {
      if (document.formname.fieldname.value > 255)
          {
                     alert("Required Field:\n lot number must be less than 255. Current value="+document.formname.fieldname.value )
                        document.formname.fieldname.focus();
                        return false;
          }
                 }

</Script>


<form action="Page.asp" method=post  name='form' ID="form" onsubmit="return Validate_Form()">
0
intreegCommented:
If you are simpy trying to limit the number of characters in the field you can set the size attribute to 255 on the input tag and it will prevent users from typing more than 255 characters. It appears that this is what your code is doing currently.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

intreegCommented:
To check to make sure the value entered is a number between 1 and 255  then you need to do something like:

myRegEx = new RegExp('^[1-255]{1,3}$')

myMatch = myRegEx.exec(document.formname.fieldname.value)

if(myMatch[0] > 255){
    alert("Required Field:\n lot number must be less than 255. Current value="+document.formname.fieldname.value )
   document.formname.fieldname.focus();
   return false;
}else{
   return true;
}
0
jessegivyDeveloperCommented:
Complexity anybody?  This is a really simple solution, regex not needed:

<script language =javascript>
function validateForm()
{
     var num=Number(document.formName.fieldName.value);
     if (num>255 || num<1)
     {
          alert("Required Field:\n lot number must be between 1 and 255)
          document.formname.fieldname.focus();
          return false;
     }
     return true;
}
</script>

...jespires was close, but you've gotta convert to a number first!  If you've got multiple fields to validate just insert a similar var and if statement right above "return true;"
0
intreegCommented:
You made the point that RegEx is more complex, maybe in theory but 8 lines compared to 6 lines doesn't appear to be that much more complex. Not too mention is more accurate and if done properly can protect against injection based on different codepages a lot more effectivley than a "simple" number comparison. With that said it may or maynot make a difference depending on the context in which you are trying to validate input.
One more thing, a mistake Jesse and I both made, and a very important one at that!

We are returning true for everything except what we consider to be invalid (regardless of how the input is tested). The best practice is to return true only when the input is determined to be valid. This is a small difference in code that can make a big difference in quality (and security).

Exmaple:
<script language =javascript>
function validateForm()
{
     var num=Number(document.formName.fieldName.value);
     if (num<256 && num>0)
     {
           return true;
     }else{
          alert("Required Field:\n lot number must be between 1 and 255)
          document.formname.fieldname.focus();
          return false;
     }
}
</script>


... just some thoughts ...
0
ramsin112400Author Commented:
I don't know for some reason the form field is not validating..not getting where i'm doing mistake ...the user entered value must be between 1 and 255....this is the code

function validateSIPForm()
{
     var num=Number(document.sIPRemoteHostForm.remoteHostID.value);
                      
     if (num<256 && num>0)
     {
                     return true;
     }
     else
     {
          document.sIPRemoteHostForm.remoteHostID.focus();
          alert("Required Field:\n lot number must be between 1 and 255)
          return false;
     }
}

I'm calling the function like this ::
--------------------------------------
<td  align="center" ><b><html:submit        styleClass="demo3" onmouseover="hov(this,'demo3 btnhov')" onmouseout="hov(this,'demo3')"  property="action"       onclick="validateSIPForm()" value="Load"  /></b> </td>       
0
jespiresCommented:
I think this should work, But do you have the function inside script tags?  Also I normally do a OnSubmit="return validateSIPForm()" in the Form tag and not with the submit button.  

Also the problem may just be with the "return" not being there since if you don't meet the criteria of the validation it will still post the form without the return after the validation.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
intreegCommented:
Try this:

<script language ="javascript">
function validateSIPForm()
{
     var num=Number(document.formName.fieldName.value);
     if (num<256 && num>0)
     {
           alert(num) //For testing value

           document.formname.submit()
     }else{
          alert("Required Field:\n lot number must be between 1 and 255)
          document.formname.fieldname.focus();

     }
}
</script>

.....

<td  align="center" ><b><html:submit       styleClass="demo3" onmouseover="hov(this,'demo3 btnhov')" onmouseout="hov(this,'demo3')"  property="action"      onclick="validateSIPForm()" value="Load"  /></b> </td>    
0
intreegCommented:
Keep in mind using the above pattern you would need to remove the OnSubmit stuff from your form tag and verify that any objecting trying to submit your form needs to call your function and not the submit function of the form itself.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Servers

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.