0

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?

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by Misklahr
0

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>
0

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.

Edited by Misklahr: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.