Hi.

I have a problem with some JS at my website.

The user has to fill out a form, enter his/her name, e-mail, phonenumber, location etc.
The problem is that I don't want to have a fake phonenumber entered to my database, so I want to verify the phonenumber by comparing the area codes of my country (Sweden).

I have all the area codes, (2, 3 or 4 digits) but dont know how to take the entered value and see if the first characters matches the area codes.

function validateForm() {

var msg = "Check:\n\n";

var name=document.forms["quest"]["name"].value
if (name==null || name==""){
  msg += " - Name\n";
  }

var tele=document.forms["quest"]["tele"].value
var telelangd = tele.length;
if (tele==null || tele=="" || isNaN(tele) || telelangd < 8)

  msg += " - Telephonenumber\n";

//continues with e-mail etc...

msg = A warning message. If the user types something wrong, it will show when msg is alerted after clicking the "check" button.

Tried to understand startWith(), but it failed.

Can anyone help me?

By using subtring(), you can extract the first four digits. please try this.

<input type="text" id="some_field">
<input type="button" id="some_button" value="Click" onclick="callFun()">

<script>
	function callFun()
	{
		var str=document.getElementById("some_field").value;
		var FirstFour=str.substring(0,4);
		alert(FirstFour);
	}
</script>

By using subtring(), you can extract the first four digits.

Does it work if the numbers are for example:
0413, 0929, 08, 019

What I mean is that they all not are 4 digit numbers.
Then see if any of the numbers matches the input.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.