Jump to content

Validating field entries as a number range


Recommended Posts

I want to validate a field entry (called 'Big' in the example below) on a submission DataPage to be in a specific numeric range (between 3 and 48 in the example below).

I hoped that I could get an error message to appear saying 'value not in the correct range' once the user clicks Submit, similar to the 'required field' message.

I have the following code:

function validateTextNumericInRange ('InsertRecordBig') {

var textInput = document.getElementById('InsertRecordBig');

var value = parseInt(textInput.value, 10);

return (!isNaN(value) && value >= 3 && value <= 48);

}

I am sure I have coding errors - I can't quite work out how Caspio deals with this issue. Please help!

Link to comment
Share on other sites

Hi,

Insert code into the footer of your DataPage

<script type="text/javascript">
function ValidateNumbers()
{
var n=document.getElementById("InsertRecordBig").value;
if(n<3||n>48)
{
alert("Value not in the correct range. Please enter number between 3 and 48.");
return true;
}
}
document.getElementById("Submit").onclick = function()
{
if (!ValidateNumbers()) 
{
this.submit();
}
else
{
return false;
}
} 
</script>

By the way, you can do this without JS :) You can just select Dropdown or Listbox form element for this field and enter custom values. :)

Let me know if this helps.

Link to comment
Share on other sites

Many thanks, it works BUT if I try to validate another field on the same DataPage it does not work -

ie. I duplicated the code and replaced the field name and message, but I get the second alert message for data entered out of range in the first field.

I tried to rename the function as something else but that didn't help.

I don't want to use Dropdown option as the range of allowed values can be huge for some of the fields.

Can you help with validating for more than one field?

Thanks.

Link to comment
Share on other sites

  • 2 weeks later...

Hi,

<script type="text/javascript">
function ValidateNumbers()
{
var n=document.getElementById("YourFirstNumberFieldID").value;
if(n<3||n>48)
{
alert("Value not in the correct range. Please enter number between 3 and 48.");
return true;
}
}
function ValidateNumbers2()
{
var n=document.getElementById("YourSecondNumberFieldID").value;
if(n<3||n>48)
{
alert("Value not in the correct range. Please enter number between 3 and 48.");
return true;
}
}
document.getElementById("Submit").onclick = function()
{
if (!ValidateNumbers()&&!ValidateNumbers2())
{
this.submit();
}
else
{
return false;
}
}
</script>

And replace YourFirstNumberFieldID and YourSecondNumberFieldID with correct field ids. Also change range for validation of the second field.

Let me know if this helps.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...