Jump to content

JS to check if string numeric, display alert and not submit


Recommended Posts

Hi, I've created a script that takes a 'MMYY' date in a string (Version_Date) and creates a new field in 'YYMM' format (Logical_Date). The input has to be a string not a number (to capture leading zeros such as 0612).

I need to add some validation to check if the 'Version_Date' field (MMYY) is numeric, if not - alert the user and then return to the submission form. If the string is numeric then the JS continues and switches MMYY to YYMM etc.

The JS is as follows

function concatenate()

{

var Version_Date = document.getElementById("InsertRecordVersion_Date").value;

'''''''''''''''''''''''''''''''''''''' I'm guessing the validation needs to go here somehow :/

var URN = document.getElementById("InsertRecordURN").value;

document.getElementById("InsertRecordConcat_URN").value = URN + "_" + Version_Date;

var MM = Version_Date.substr(0,2)

var YY = Version_Date.substr(2,2)

document.getElementById("InsertRecordLogical_Date").value = YY + MM;

}

document.getElementById("caspioform").onsubmit=concatenate;

Any assistance / comments very welcome.

Thanks

Mat

Link to comment
Share on other sites

I've managed to get this working but my JS is not skilled at all.

This works but is probably not coded very well.

function Validate()

{

var Version_Date = document.getElementById("InsertRecordVersion_Date").value;

var ValidChars = "0123456789";

var IsNumber=true;

var Char;

for (i = 0; i < Version_Date.length && IsNumber == true; i++)

{

Char = Version_Date.charAt(i);

if (ValidChars.indexOf(Char) == -1)

{

alert("Non-Numeric Version Date");

IsNumber = false;

}

else

{

var Version_Date = document.getElementById("InsertRecordVersion_Date").value;

var URN = document.getElementById("InsertRecordURN").value;

document.getElementById("InsertRecordConcat_URN").value = URN + "_" + Version_Date;

var MM = Version_Date.substr(0,2)

var YY = Version_Date.substr(2,2)

document.getElementById("InsertRecordLogical_Date").value = YY + MM;

}

}

return IsNumber;

}

document.getElementById("caspioform").onsubmit=Validate;

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...
 Share

×
×
  • Create New...