I am new to JavaScript and I have been doing some work creating a form in HTML and JavaScript. In this work I have been trying to validate the format of an input depending on the text entered into a previous field.
What i have been trying is if the country 'Australia' is entered into the 'Country' text box than the 'Telephone' text box is limited to the format (00)00000000 and if it is any other country than the 'Telephone' text box must be formatted in the international number way including the + and country code following e.g. +61 etc
i have done this much of a function so far:
<script>
document.getElementById('txttelephone').onchange = function()
{
var num1 = document.getElementById('txttelephone').value,
country = document.getElementById('txtcountry').value,
regex;
if (country == "Australia" || country == "australia")
{
regex = /\(\d{2}\)\d{8}/;
}
else
{
regex = /\+\d{15}/;
}
if (!num1.match(regex))
{
alert('That is not a correct telephone number');
}
}
</script>
That is just the function i did to limit the string length of the 'Telephone' text box to 12 characters but i still have yet to validate to make sure that the area code is included in between brackets in the the form of (00)00000000 and also to validate that a + is included if the country is anything other than Australia (international numbers including country codes).
Here is the HTML i have to use the function on:
<b>Country:</b> <input type="text" id="txtcountry" name="country">
<br>
<b>Telephone:</b> <input type="text" id="txttelephone" name="telephone">
Any help would be greatly appreciated!