Javascript regex that can only contain letters, numbers, dashes and must contain a dot

I am really bad at Regex but in my JavaScript app I am trying to validate a form item, the value of the form item can only contain letters, numbers, dashes and at least one dot.

So the following would be valid:

hello.world

microsoft.com

So far I came up with this (.+)[a-zA-Z0-9.-]\.[a-zA-Z0-9.-]+$ but it doesn’t work as I can add spaces. What can I do to make my Regex work? Also can I prevent the dot being the first character?

3 thoughts on “Javascript regex that can only contain letters, numbers, dashes and must contain a dot”

  1. Nicely done and writen my friend!
    I started writing very recently and realised many artricles merely rehash old ideas but addd very little of value.
    It’s terrifiic to read an enlightening article of some actual value to me, as a reader.

    It’sgoing on my list of factors I need to replicate ass a new blogger.

    Reader engagementt and content quality are king.

    Some excellent thoughts; you havve definitely got on my list of people to follow!

    Keep up the good work!
    Well done,
    Madel

    Reply

Leave a Comment